首页 新闻 搜索 专区 学院

Java的serverSocket代码为什么无法接收到第三放发回来的信息,代码哪里有问题

0
悬赏园豆:40 [待解决问题]

这是用java原生实现的局域网聊天工具, 但是无法收到第三方发回的socket消息, 求助...:

服务端代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

class Input {
  public static byte[] getInput (){
    try{
      byte[] b= new byte[100];
      byte[] result;
      System.in.read(b);
      int i = 0;
      while(true) {
        boolean boo = (b[i]==13) ;
        if( boo ) {
          break;
        };
        i++;
      };
      result = new byte[i];
      System.arraycopy(b,0,result,0,i);
      return result;
    }catch(Exception e) {
      e.printStackTrace();
    }
  return null;
  }
};


public class Server extends Input{
 ServerSocket ss ;
 Socket serverSocket;
 InputStream inStream;
 OutputStream outStream;
 
 public Server(){
  try {
   System.out.println("====================Server==================");
   ss = new ServerSocket(8090);
   serverSocket= ss.accept();
   System.out.println("--------------some guest connected----------------");
   inStream = serverSocket.getInputStream();
   outStream = serverSocket.getOutputStream();
   
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 public void input() {
  new Thread(){
    public void run() {
      while(true) {
        try{
          System.out.print(" u say --->>  ");
          byte[] outArr = getInput();
          outStream.write(outArr);
        }catch(IOException e) {
          e.printStackTrace();
        }
      }
    }
  }.start();
 }

 public void conn(){
  new Thread(){
   public void run(){
    try {
     InputStreamReader iReader = new InputStreamReader(inStream);
     BufferedReader iBufferStream = new BufferedReader(iReader);
     String inMessage = iBufferStream.readLine();
     while(true){
      System.out.println("h say<<---" + inMessage);
      inMessage = iBufferStream.readLine();
     }
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }.start();
 }
 
 public static void main(String args[]){
  Server server = new Server();
  server.conn();
  server.input();
 }
}

 

客户端代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

class Input {
  public static byte[] getInput (){
    try{
      byte[] b= new byte[100];
      byte[] result;
      System.in.read(b);
      int i = 0;
      while(true) {
        boolean boo = (b[i]==13) ;
        if( boo ) {
          break;
        };
        i++;
      };
      result = new byte[i];
      System.arraycopy(b,0,result,0,i);
      return result;
    }catch(Exception e) {
      e.printStackTrace();
    }
  return null;
  }
};

public class Client extends Input{
 Socket socket;
 InputStream inStream;
 OutputStream outStream;
 

 public Client (){
  try {
   System.out.println("-------------This is client-----------------");
   socket  = new Socket("127.0.0.1",8090);
   inStream = socket.getInputStream();
   outStream = socket.getOutputStream();

  } catch (UnknownHostException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
 public void input() {
   new Thread() {
    public void run() {
      try{
        while(true) {
          System.out.print("u say ---> ");
          byte[] outArr1 = getInput();
          outStream.write(outArr1);
        }
      }catch(Exception e) {
        e.printStackTrace();
      }
    }
   }.start();
 }

 public void conn(){
   new Thread(){
    public void run(){
     try {
      InputStreamReader inReader = new InputStreamReader(inStream);
      BufferedReader bReader = new BufferedReader(inReader);
      String inMessage = bReader.readLine();
      System.out.println("-------------got server response-----------------");
      while(true){
       System.out.println("h say <-- " + inMessage);
       inMessage = bReader.readLine();
      }
     } catch (IOException e) {
      e.printStackTrace();
     }
    }
   }.start();
   
 }
 
 public static void main(String args[]){
  Client client = new Client();
  client.conn();
  client.input();
 }
}
方方和圆圆的主页 方方和圆圆 | 初学一级 | 园豆:68
提问于:2016-02-29 16:12
< >
分享
所有回答(1)
0

看不懂你想干嘛,客户端都没有,只是一个服务器serversocket在一直等待客户端的连接请求,都没有写客户端连接服务器的代码,这个怎么实现局域网聊天呢

晴朗路 | 园豆:373 (菜鸟二级) | 2016-03-02 10:48

您好, 客户端的代码也贴上来了

支持(0) 反对(0) 方方和圆圆 | 园豆:68 (初学一级) | 2016-03-03 23:14
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册