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

关于套接字的问题
服务端如下:
package socket1;

import java.io.IOException;
import java.io.OutputStream;
import java.net.*;

public class Server
{
ServerSocket serverSocket;
Socket socket;
OutputStream out;
public Server(){
try
{
serverSocket=new ServerSocket(2121);
while(true){
socket=serverSocket.accept();

out=socket.getOutputStream();

out.write( 1234567890 );

out.close();
socket.close();
}
}
catch( IOException e )
{
e.printStackTrace();
}
}
}

客户端如下:
package socket1;
import java.io.*;
import java.net.*;

public class Client
{
Socket socket;
InputStream in;
Client(){
try
{
socket=new Socket("127.0.0.1",2121);
in=socket.getInputStream();
System.out.println( in.read() );
in.close();
socket.close();

}
catch( UnknownHostException e )
{
e.printStackTrace();
}
catch( IOException e )
{
e.printStackTrace();
}

}
}

测试类如下:
package socket1;

public class Main
{

public static void main(String[] args)
{
new Server();
new Client();

}

}
为什么没有输出呢? 请高手给予支持 谢谢


------解决方案--------------------
Java code
import java.io.IOException;
import java.io.OutputStream;
import java.net.*;

public class Server {
    ServerSocket serverSocket;

    Socket socket;

    OutputStream out;

    public Server() {
        try {
            serverSocket = new ServerSocket(2121);
            while (true) {
                socket = serverSocket.accept();

                out = socket.getOutputStream();

                out.write(1234567890);

                out.close();
                socket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        new Server();
    }
}