刚编一个简单网络通讯程序,运行不通过,希望高手指教
刚编了一个网络通讯简单程序总是报如下的错误,这里面头二行是我加入的提示信息,没有什么作用、
Remote Server addr is:localhost/127.0.0.1
Making Client0
cant make client Socket
java.lang.NullPointerException
at TcpClientThread.<init>(TcpClient.java:23)
at TcpClient.main(TcpClient.java:71)
Exception in thread "main" Exit code: 1
There were errors
但是我反复的查了TcpClientThread的构造函数实在是看不出有什么问题。
原程序如下
import java.net.*;
import java.io.*;
class TcpClientThread extends Thread{
private Socket s;
private static int count=0;
private int id=count++;
private int identifier;
private BufferedReader in;
private PrintWriter out;
public TcpClientThread(InetAddress addr)
{
System.out.println("Making Client"+id);
try{
s=
new Socket(addr,8080);
}catch(IOException e){
System.out.println("cant make client Socket");
}
try{
in=new BufferedReader(
new InputStreamReader(
s.getInputStream()));
out=new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
s.getOutputStream())),true);
start();
}catch(IOException e){
System.out.println("cant't make in and out Stream");
try{
s.close();
}catch(IOException e2){
System.out.println("can't correctly close client socket");
}
}
}
public void run(){
try{
for(int i=0;i<2;i++){
String str=new String("client #"+id+" Message #"+i);
out.println(str);
System.out.println(str);
str=in.readLine();
System.out.println(str);
}
out.println("end");
}catch(IOException e){
}finally{
try{
s.close();
System.out.println("Client socket is Closing");
}catch(IOException e){}
}
}
}
public class TcpClient {
public static void main(String args[])
throws IOException ,InterruptedException
{
InetAddress addr= InetAddress.getByName(null);
System.out.println("Remote Server addr is:"+addr);
new TcpClientThread(addr);
}
}