在下面这个Socket编程中,为什么TCPServer运行不出来,而TCPClient能运行出来
TCPServer代码:importjava.net.*;importjava.io.*;publicclassTCPServer{publicstaticvoidmai...
TCPServer代码:
import java.net.*;import java.io.*;
public class TCPServer { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(6666); //这句为什么有问题? while(true){ Socket s = ss.accept(); System.out.println("aaaaa"); DataInputStream dis = new DataInputStream(s.getInputStream()); System.out.println(dis.readUTF()); dis.close(); ss.close(); s.close(); } }}
TCPClient代码:
import java.net.*;import java.io.*;
public class TCPClient { public static void main(String[] args) throws Exception { Socket s = new Socket("127.0.0.1",6666); OutputStream os = s.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); dos.writeUTF("hello server1234"); dos.flush(); dos.close(); s.close(); }}
TCPServer的运行结果: 展开
import java.net.*;import java.io.*;
public class TCPServer { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(6666); //这句为什么有问题? while(true){ Socket s = ss.accept(); System.out.println("aaaaa"); DataInputStream dis = new DataInputStream(s.getInputStream()); System.out.println(dis.readUTF()); dis.close(); ss.close(); s.close(); } }}
TCPClient代码:
import java.net.*;import java.io.*;
public class TCPClient { public static void main(String[] args) throws Exception { Socket s = new Socket("127.0.0.1",6666); OutputStream os = s.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); dos.writeUTF("hello server1234"); dos.flush(); dos.close(); s.close(); }}
TCPServer的运行结果: 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询