//服务器,多线程处理机制import java.net.*;
import java.io.*;
class ServerThread extends Thread
{
private Socket s;
private BufferedReader br;
private PrintWriter pw;
public ServerThread(Socket s) throws IOException
{
this.s=s;
System.out.println("Connection accepted:"+s);
//InputStream is=;
InputStreamReader isr=new InputStreamReader(s.getInputStream());
BufferedReader br=new BufferedReader(isr);
//br.readLine();
OutputStreamWriter osw=new OutputStreamWriter(s.getOutputStream());
BufferedWriter bw=new BufferedWriter(osw);
PrintWriter pw=new PrintWriter(bw,true);
start();
}
public void run()
{
try
{
while(true)
{
String str=br.readLine();
if(str.equals("end")) break;
System.out.println("encoding"+str);//服务器自己这边打印
//String str1="welcome ,This is server!";
//pw.println(str1);
pw.println(str);//写入客户端
}
System.out.println("closing...");
}catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
s.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}public class MyServer
{
public static void main(String args[]) throws IOException
{
ServerSocket ss=new ServerSocket(8080);
System.out.println("started:"+ss);
try
{
while(true)
{
Socket s=ss.accept();
try
{
ServerThread st=new ServerThread(s);
}catch(Exception y)
{
y.printStackTrace();
}
finally
{
s.close();
}
}
//st.start();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
ss.close();
}
}
}
这个代码我检查了很多遍,但是运行总是提示:java.lang.NullPointerException
at ServerThread.run<MyServer.java:30>
请哪位高手解答一下,提前谢过了!
import java.io.*;
class ServerThread extends Thread
{
private Socket s;
private BufferedReader br;
private PrintWriter pw;
public ServerThread(Socket s) throws IOException
{
this.s=s;
System.out.println("Connection accepted:"+s);
//InputStream is=;
InputStreamReader isr=new InputStreamReader(s.getInputStream());
BufferedReader br=new BufferedReader(isr);
//br.readLine();
OutputStreamWriter osw=new OutputStreamWriter(s.getOutputStream());
BufferedWriter bw=new BufferedWriter(osw);
PrintWriter pw=new PrintWriter(bw,true);
start();
}
public void run()
{
try
{
while(true)
{
String str=br.readLine();
if(str.equals("end")) break;
System.out.println("encoding"+str);//服务器自己这边打印
//String str1="welcome ,This is server!";
//pw.println(str1);
pw.println(str);//写入客户端
}
System.out.println("closing...");
}catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
s.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}public class MyServer
{
public static void main(String args[]) throws IOException
{
ServerSocket ss=new ServerSocket(8080);
System.out.println("started:"+ss);
try
{
while(true)
{
Socket s=ss.accept();
try
{
ServerThread st=new ServerThread(s);
}catch(Exception y)
{
y.printStackTrace();
}
finally
{
s.close();
}
}
//st.start();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
ss.close();
}
}
}
这个代码我检查了很多遍,但是运行总是提示:java.lang.NullPointerException
at ServerThread.run<MyServer.java:30>
请哪位高手解答一下,提前谢过了!
{ServerThread st=new ServerThread(s);
}catch(Exception y)
{
y.printStackTrace();
}
finally
{
s.close();
}
socket传给ServerThread后,就把socket关掉,就不能进行通信了吧?
把s.close()这句话去掉吧,在ServerThread里关就行了。
LZ把客户端代码也贴出来吧
import java.io.*;
class ClientThread extends Thread
{
private Socket s;
private BufferedReader in;
private PrintWriter out;
private static int counter=0;
private int id=counter++;
private static int threadcount=0;
public static int threadCount()
{
return threadcount;
}
public ClientThread(InetAddress addr) throws IOException
{
System.out.println("making client"+id);
threadcount++;
try
{
s=new Socket(addr,8080);
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream())),true);
start();
}catch(Exception ex)
{
ex.printStackTrace();
}
}catch(Exception e)
{
e.printStackTrace();
}finally
{
try
{
s.close();
}
catch(Exception y)
{
y.printStackTrace();
}
}
//System.out.println("scoket:"+s);
}
public void run()
{
try
{
for(int i=0;i<25;i++)
{
out.println("Client"+id+":"+i);
String str=in.readLine();
System.out.println(str);
}
out.println("end");
}catch(Exception e)
{
e.printStackTrace();
}finally
{
try {
s.close();
}catch(Exception exy)
{
}
threadcount--;
}
}
}
public class MyClient
{
public static void main(String args[]) throws IOException,InterruptedException
{
InetAddress addr=InetAddress.getByName(null);
while(true)
{
if(ClientThread.threadCount()<40)
new ClientThread(addr);
Thread.currentThread().sleep(100);
}
}
}