简体   繁体   English

客户-服务器套接字编程

[英]CLIENT -SERVER SOCKET PROGRAMMING

I have written client-server message sending program using sockets in Java, which is working fine, but if the string length of the previous message is greater than the present in.read(b5) , string message is printed with previous message characters added to it. 我已经使用Java用套接字编写了客户端-服务器消息发送程序,效果很好,但是如果上一条消息的字符串长度大于当前in.read(b5) ,则会打印字符串消息,并在其中添加先前的消息字符它。 This problem is due to previous stored characters in byte array. 此问题是由于以前在字节数组中存储的字符。

How to clear the byte array for every loop? 如何清除每个循环的字节数组? How can I resolve this? 我该如何解决?

CLIENT code: 客户代码:

try
  {
    String add=args[0];
    int port=Integer.parseInt(args[1]);
    Socket s=new Socket(add,port);
    OutputStream out=s.getOutputStream();
    InputStream in=s.getInputStream();
    BufferedReader bufIn = new BufferedReader( new InputStreamReader( in ) );
    BufferedWriter bufOut = new BufferedWriter( new OutputStreamWriter( out ) );
    byte b1[]=new byte[100];
    byte b2[]=new byte[100];
    while(true)//infinite loop
    {
      System.out.println("To Server:");
      System.in.read(b1);  //reading from keyboard
      out.write(b1);       //writing to server
      in.read(b2);         //reading from server            
      String s1=new String(b2);
      String s2=s1.trim();
      System.out.println("From Server:"+s2);
    }   
  }
  catch (Exception e)
  {
    System.err.println(e);
  }

SERVER code: 服务器代码:

try
{       
  int port=Integer.parseInt(args[0]);
  ServerSocket ss=new ServerSocket(port);
  while(true)   //this loop for diff. no. of clients
  {
    Socket s=ss.accept();               
    InputStream in=s.getInputStream();
    OutputStream out=s.getOutputStream();
    byte b1[]=new byte[100];
    byte b2[]=new byte[100];
    while(true)   //this loop for one client diff. no. of times.
    {                   
      in.read(b1);
      String s1=new String(b1);
      String s2=s1.trim();
      System.out.println("From Client:"+s2);
      System.out.print("To Client:");
      System.in.read(b2);
      out.write(b2);
    }
  } 
}
catch (Exception e)
{
  System.err.println(e);
}

add out.flush(); 添加out.flush(); to the bottom of your loop. 到循环的底部。 Read the javadoc for more info. 阅读javadoc以获得更多信息。 See relevant part below: 请参阅以下相关部分:

"Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination. " “刷新此输出流并强制将所有缓冲的输出字节写出。刷新的一般约定是,调用它表明,如果先前写入的任何字节已由输出流的实现缓冲,则此类字节应立即执行。被写到预定的目的地。”

you need to read till the stream is completely read. 您需要阅读直到流被完全阅读。 The same is applicable for write also 同样适用于写

For writing/sending 用于写/发送

System.out.println("To Server:");
            while (System.in.read(b1) != -1)
            {
                out.write(b1);// writing to server
            }

For reading 阅读

                   String s2 = "";

                do
                {
                    in.read(b1);
                    String s1 = new String(b1);
                    s2 += s1;
                } while (in.available() > 0);

It's related to the way you use b1 and b2 arrays. 这与您使用b1和b2数组的方式有关。 You are reusing the same array for every read, so the bytes that are not overridden by present read remain unchanged. 您将为每次读取重用同一数组,因此当前读取不会覆盖的字节保持不变。

See Javadoc for InputStream.read: 有关InputStream的信息,请参见Javadoc:

The first byte read is stored into element b[0], the next one into b 1 , and so on. 读取的第一个字节存储在元素b [0]中,下一个字节存储在b 1中 ,依此类推。 The number of bytes read is, at most, equal to the length of b. 读取的字节数最多等于b的长度。 Let k be the number of bytes actually read; 令k为实际读取的字节数; these bytes will be stored in elements b[0] through b[k-1], leaving elements b[k] through b[b.length-1] unaffected. 这些字节将存储在元素b [0]至b [k-1]中,而元素b [k]至b [b.length-1]不受影响。

You can't use Arrays.fill(b1,0) because b1 is an array of bytes and 0 is a int. 您不能使用Arrays.fill(b1,0),因为b1是字节数组,而0是整数。 That is you are pretending to use Arrays.fill(byte[] b, int val) that does not exist. 那就是您假装使用不存在的Arrays.fill(byte [] b,int val)。

Just cast 0 to byte and you are done: 只需将0强制转换为字节,即可完成操作:

Arrays.fill(b1, (byte)0);

In this case you are using method Arrays.fill(byte[] b, byte val) that does exist. 在这种情况下,您将使用确实存在的方法Arrays.fill(byte [] b,byte val)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM