刚编一个简单网络通讯程序,运行不通过,希望高手指教

newgoodboy 2001-11-20 08:23:14
刚编了一个网络通讯简单程序总是报如下的错误,这里面头二行是我加入的提示信息,没有什么作用、
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);
}
}
...全文
59 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
狐帝 2001-11-20
  • 打赏
  • 举报
回复
你调用s=new Socket(addr,8080);初始化的时候,无法建立这个SOCKET,所以s对象为null。后面那一堆异常信息就是表示这个错误。

从代码来看,应该是本机的8080端口已经被占用。
cherami 2001-11-20
  • 打赏
  • 举报
回复
InetAddress addr= InetAddress.getByName(null);
这个有问题!!!!!
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,80);
}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("127.0.0.1");
System.out.println("Remote Server addr is:"+addr);

new TcpClientThread(addr);
}
}

你把端口打开,我在80端口打开了apache服务。运行结果如下:
Remote Server addr is:127.0.0.1/127.0.0.1
Making Client0
client #0 Message #0

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧