主题:Socket问题
服务端
import java.net.*;
import java.io.*;
public class MyServer
{
public static void main(String[] args) throws Exception
{
ServerSocket server = null;
Socket client = null;
PrintStream out = null;
server = new ServerSocket(8888);
System.out.println("服务器运行,等待客户端");
client = server.accept();
String str = "Hello World!";
out = new PrintStream(client.getOutputStream());
out.println(str);
out.close();
client.close();
}
}
客户端
import java.net.*;
import java.io.*;
public class MyClient
{
public static void main(String[] args) throws Exception
{
Socket client = null;
client = new Socket("localhost", 8888);
BufferedReader buf = null;
buf = new BufferedReader(new InputStreamReader(client.getInputStream()));
String str = buf.readLine();
System.out.println("服务器的输出内容: " + str);
client.close();
buf.close();
}
}
但是没有任何输出结果,服务端始终显示“服务器运行,等待客户端”,客户端始终显示”服务器的输出内容:”+空白,请问原因
import java.net.*;
import java.io.*;
public class MyServer
{
public static void main(String[] args) throws Exception
{
ServerSocket server = null;
Socket client = null;
PrintStream out = null;
server = new ServerSocket(8888);
System.out.println("服务器运行,等待客户端");
client = server.accept();
String str = "Hello World!";
out = new PrintStream(client.getOutputStream());
out.println(str);
out.close();
client.close();
}
}
客户端
import java.net.*;
import java.io.*;
public class MyClient
{
public static void main(String[] args) throws Exception
{
Socket client = null;
client = new Socket("localhost", 8888);
BufferedReader buf = null;
buf = new BufferedReader(new InputStreamReader(client.getInputStream()));
String str = buf.readLine();
System.out.println("服务器的输出内容: " + str);
client.close();
buf.close();
}
}
但是没有任何输出结果,服务端始终显示“服务器运行,等待客户端”,客户端始终显示”服务器的输出内容:”+空白,请问原因