我正在學習有關服務器服務器的多個消息 - 客戶溝通,我做了一個簡單的溝通,它的工作原理,但我只能發送一個消息給服務器。我不知道如何發送和接收來自客戶端的更多消息。我嘗試了很多選擇,但都沒有成功。爪哇 - 客戶 - 從客戶
這是我的代碼: 客戶端: 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();
}
}