我正在学习有关服务器服务器的多个消息 - 客户沟通,我做了一个简单的沟通,它的工作原理,但我只能发送一个消息给服务器。我不知道如何发送和接收来自客户端的更多消息。我尝试了很多选择,但都没有成功。爪哇 - 客户 - 从客户
这是我的代码: 客户端: import java.io. ; import java.net。;
public class Klient
{
public static final int PORT=50007;
public static final String HOST = "127.0.0.1";
public static void main(String[] args) throws IOException
{
Socket sock;
sock=new Socket(HOST,PORT);
System.out.println("communication works: "+sock);
BufferedReader klaw;
klaw=new BufferedReader(new InputStreamReader(System.in));
PrintWriter outp;
outp=new PrintWriter(sock.getOutputStream());
System.out.print("<Sending:> ");
String str=klaw.readLine();
outp.println(str);
outp.flush();
klaw.close();
outp.close();
sock.close();
}
}
和服务器:
import java.io.*;
import java.net.*;
public class Serwer
{
public static final int PORT=50007;
public static void main(String args[]) throws IOException
{
ServerSocket serv;
serv=new ServerSocket(PORT);
System.out.println("listen for: "+serv);
Socket sock;
sock=serv.accept();
System.out.println("communication: "+sock);
BufferedReader inp;
inp=new BufferedReader(new InputStreamReader(sock.getInputStream()));
String str;
str=inp.readLine();
System.out.println("<it comes:> " + str);
inp.close();
sock.close();
serv.close();
}
}