这是用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(); } }
看不懂你想干嘛,客户端都没有,只是一个服务器serversocket在一直等待客户端的连接请求,都没有写客户端连接服务器的代码,这个怎么实现局域网聊天呢
您好, 客户端的代码也贴上来了