A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

class tcpSocket {
public static void main(String[] args) throws UnknownHostException, IOException {
  Socket soc=new Socket("10.10.1.172", 10001);  
  
  OutputStream out=soc.getOutputStream();
  
  BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
  
  //InputStream input=soc.getInputStream();
  String line=null;
  while((line=read.readLine())!=null){
   if("886".equals(line))
    break;
    out.write(line.getBytes());
   // byte[] buf=new byte[1024];
   
   // int len=input.read(buf);
    //System.out.println(new String(buf,0,len));
  }
  read.close();
  soc.close();
}
}


class tcpServer {
public static void main(String[] args) throws IOException {
  ServerSocket server=new ServerSocket(10001);
  
  Socket soc=server.accept();
  
  String s=soc.getInetAddress().getHostAddress();
  
  System.out.println("已连接"+s);
  
  InputStream input=soc.getInputStream();
  
  byte[] buf=new byte[1024];
  int len=input.read(buf);
  System.out.println(new String(buf,0,len));
  
  /*
  OutputStream out=soc.getOutputStream();
  
  BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
  
  String line=null;
  
  while((line=read.readLine())!=null){
   if("886".equals(line))
    //break;  
   byte[] by=new byte[1024];
   out.write(line.getBytes());
   }
  */
  
  //read.close();
  soc.close();
}
}


报错:Exception in thread "main" java.net.SocketException: Software caused connection abort: socket write error

1 个回复

倒序浏览
将服务端改成这样就不会了
  1. public class TcpServer {
  2. public static void main(String[] args) throws IOException {
  3. ServerSocket server = new ServerSocket(10001);

  4. Socket soc = server.accept();

  5. String s = soc.getInetAddress().getHostAddress();

  6. System.out.println("已连接" + s);

  7. InputStream input = soc.getInputStream();

  8. byte[] buf = new byte[1024];
  9. int len = 0;
  10. while((len = input.read(buf))!=-1)
  11. System.out.println(new String(buf, 0, len));


  12. }
  13. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
SyouRai_Tsk + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马