日期:2014-05-20  浏览次数:20644 次

网络编程问题
在这段代码中,为什么server打印不出任何东西??

import java.net.*;
import java.io.*;
public class Server{
public static void main(String[] ages){
ServerSocket ss = null;
Socket s2 = null;
try{
ss = new ServerSocket(6666);
}catch(IOException io){}
try{
s2 = ss.accept();
System.out.println("欢迎光临");
BufferedReader b2 = new BufferedReader(new InputStreamReader(s2.getInputStream()));

String s = b2.readLine();
while(!s.equals("bye")){
System.out.print(s);
}

}catch(IOException io){}
}
}

import java.net.*;
import java.io.*;
public class Client{
public static void main(String[] ages) throws Exception{
Socket s1 = new Socket("127.0.0.1",6666);
BufferedReader b1 = new BufferedReader(new InputStreamReader(System.in));
String s = b1.readLine();


}
}

------解决方案--------------------
Client 要 Output

不然何来 Server 的数据
------解决方案--------------------
Try this :

SocketServer
Java code

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

public class SocketServer {

    public static void main(String[] args) {
        ServerSocket ss = null;
        Socket s = null;
        try {
            ss = new ServerSocket(1234);
            s = ss.accept();

            BufferedReader br = new BufferedReader(new InputStreamReader(s
                    .getInputStream()));
            String line;

            while (true) {
                line = br.readLine();
                if (line != null){
                    System.out.println(line);
                    if(line.equalsIgnoreCase("bye"))
                        break;
                }
                else
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                s.close();
                ss.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}