问答详情
源自:3-5 使用多线程实现多客户端的通信

希望看一下我在这个到底有什么问题 找一天了。。。


package come.lala;


import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;


public class Server {


public static void main(String[] args)  {

try {

ServerSocket serversocket=new ServerSocket(8888);

Socket socket=null;

int count=0;

//调用accept()方法 对指定端口进行侦查 等待客户端输入

System.out.println("***服务器等待开启***");

while(true){

        socket=serversocket.accept();

        //创建一个新的线程 

    ServerThread serverThread=new ServerThread(socket);

        serverThread.start();

            count++;

        System.out.println("客户端的数量为:"+count);

}


} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


}

   

}

package come.lala;


import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.PrintWriter;

import java.net.Socket;

import java.net.UnknownHostException;


public class Client {


public static void main(String[] args) throws Exception {

try {

Socket socket = new Socket("192.168.1.100", 8888);

OutputStream os=socket.getOutputStream();

PrintWriter pw=new PrintWriter(os);

pw.write("帐号:admin;密码:123");

socket.shutdownOutput();

InputStream is=socket.getInputStream();

InputStreamReader isr =new InputStreamReader(is);

BufferedReader br=new BufferedReader(isr);

String a=null;

while((a=br.readLine())!=null){

System.out.println("我是客户端,服务端说:"+a);

}

socket.shutdownInput();

pw.flush();

os.close();

br.close();isr.close();

is.close();

socket.close();

} catch (UnknownHostException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


}


}

package come.lala;


import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.PrintWriter;

import java.net.Socket;


public class ServerThread extends Thread {

  Socket socket=null;


 public ServerThread(Socket newSocket) {

this.socket=newSocket;

}


public void run (){

    InputStream is=null;

    InputStreamReader isr=null;

    BufferedReader br=null;

    OutputStream os=null;

    PrintWriter pw=null;

    

try {

is = socket.getInputStream();

isr =new InputStreamReader(is);

br=new BufferedReader(isr);

String s=null;

while((s=br.readLine())!=null){

System.out.println(s);

}

socket.shutdownInput();

os=socket.getOutputStream();

pw=new PrintWriter(os);

pw.write("欢迎您客户端");

pw.flush();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

try {

if(pw!=null)

   pw.close();

if(os!=null)

   os.close();

if(socket!=null)

   socket.close();

if(is!=null)

       is.close();

   if(br!=null)

       br.close();

   if(isr!=null)

   isr.close();

} catch (IOException e) {

e.printStackTrace();

}

}

  }

}


提问者:MMpc 2015-10-17 16:24

个回答

  • needself
    2015-10-24 11:43:20

    建议 trycatch了就不要有throws Exception,你的client端代码更换一下。

    然后把client端 改为Socket socket = new Socket("localhost",8888);  别用IP地址

    这样试一下  可以就是这两个问题 不可以 把报错的内容发出来