2017-02-01 64 views
0

我正在学习有关服务器服务器的多个消息 - 客户沟通,我做了一个简单的沟通,它的工作原理,但我只能发送一个消息给服务器。我不知道如何发送和接收来自客户端的更多消息。我尝试了很多选择,但都没有成功。爪哇 - 客户 - 从客户

这是我的代码: 客户端: 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();               
    }                   
} 

回答

0

您需要添加一个主循环为您的代码和特殊命令退出

例如:

// infinite loop 
while (true) { 

    // ..receive or send commands here.. 

    if (command.equals("exit") { 
    // exit from loop 
    } 

} 

同时添加异常处理(的try-catch-finally程序),或者你的应用程序将是非常脆弱的

0

TCP套接字流中的发送数据。 TCP不支持在“消息”或“块”中发送数据。你在代码中做什么是发送和接收流。

要发送“消息”使用TCP,应用协议必须在TCP的顶部限定。这个协议应该有能力发送“消息”。 (如果你不明白这部分你应该阅读有关的协议层,7层OSI模型,和5层TCP/IP套件)

甲方式做到这一点是定义一个消息终止字符。该流将如下所示:

<message><termination-character><message><termination-character> 

终止符是来自消息字符集的字符或其外部的字符。在后一种情况下,消息中终止字符的任何出现都应该用换码序列替换。

假设我们使用“\ n”作为终止字符,我们假设“\ n”是不是在消息的字符集。您的客户端应该是这样的:

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 final char TERMINATIONCHAR = '\n'; 

    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());          

     //define the loop 
     while(true){ 
      System.out.print("<Sending:> ");             
      String str=klaw.readLine(); 
      outp.print(str+TERMINATIONCHAR);                
      outp.flush(); 
     } 

     /* uncomment if the loop can be exited 
     klaw.close();                  
     outp.close();                  
     sock.close();*/ 
    }                     
} 

,你的服务器应该是这样的:

import java.io.*; 
import java.net.*; 

public class Server 
{ 
    public static final int PORT=50007; 
    public static final char TERMINATIONCHAR = '\n'; 

    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())); 

     //define the loop 
     while(true){ 
      String str;                
      str=inp.readLine();            
      System.out.println("<it comes:> " + str); 
     } 

     /* uncomment if the loop can be exited 
     inp.close();               
     sock.close();               
     serv.close();*/                 
    }                   
}