当前位置: 技术问答>java相关
刚编一个简单网络通讯程序,运行不通过,希望高手指教
来源: 互联网 发布时间:2015-02-22
本文导语: 刚编了一个网络通讯简单程序总是报如下的错误,这里面头二行是我加入的提示信息,没有什么作用、 Remote Server addr is:localhost/127.0.0.1 Making Client0 cant make client Socket java.lang.NullPointerException at TcpClientThread.(TcpCli...
刚编了一个网络通讯简单程序总是报如下的错误,这里面头二行是我加入的提示信息,没有什么作用、
Remote Server addr is:localhost/127.0.0.1
Making Client0
cant make client Socket
java.lang.NullPointerException
at TcpClientThread.(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
Remote Server addr is:localhost/127.0.0.1
Making Client0
cant make client Socket
java.lang.NullPointerException
at TcpClientThread.(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