开发者

Java, Socket and transfer text

I have one problem, I learn how work with socket and I write programm with next code :

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

public class Client extends Thread{

        public static void main(String args[])
        {
            try
            {

                Socket s = new Socket(args[0],Integer.parseInt(args[1]));
                BufferedReader br=new BufferedReader(new InputStreamReader(System.in,"CP866"));
                String str;
                str=br.readLine();
                str = str+"\n"+s.getInetAddress().getHostAddress()
                +":"+s.getLocalPort();
                PrintWriter pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
                pw.println(str);
                pw.flush();
                while(true){
                    str=null;
                    if((str=br.readLine())==null)
                    break;
                    str = str+"\n"+s.getInetAddress().getHostAddress()
                    +":"+s.getLocalPort();
                    System.out.p开发者_开发知识库rintln(str);
                pw.print(str);
                pw.flush();
                }
            }
            catch(Exception e)
            {System.out.println("init error: "+e);} 
        }

}

Programm of server-side just print obtained message, first message normal print, but next message print in format :, when I cut 25-26 line - message not sent or not obtained.

Why?


You wrote print(str);. So you don't print a newline(\n). Your server-side uses also a BufferedReader (I supose). And he is reading until a newline.

So change to println(str);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜