import java.net.*;
import java.io.*;
public class Jsever extends Thread{
private Socket sock;
private int id;
DataOutputStream dout;
DataInputStream din;
String ss;
char c;
public Jsever(Socket s,int i){sock=s;id=i;}
public void run()
{
try{
System.out.println("socket.."+id+"..connection success");
dout=new DataOutputStream(sock.getOutputStream());
dout.writeUTF("sock.."+id+"..welcome client");
while(true)
{
din=new DataInputStream(sock.getInputStream());
String s=din.readUTF();
System.out.println("sock.."+id+"..recvice.."+s);
}
} catch(Exception e){System.out.println("err1");}
}
public static void main(String d[])
{
int n=1;
ServerSocket sever=null;
try{
sever=new ServerSocket(6000);
System.out.println("sever start");
}
catch(IOException r){}
while(true){
try{
System.out.println("wait no."+n+"connect");
Socket s=sever.accept();
Jsever t=new Jsever(s,n++);
t.start();
}catch(IOException ee){}
}}
}
这样的一段程序我在命令提示符中运行正常!为什么在eclipse就出现java.lang.NoClassDefFoundError: Jsever
Exception in thread "main"
----------------解决方案--------------------------------------------------------
你用ECLIPSE编译一下再运行。
----------------解决方案--------------------------------------------------------
编译正常就是运行不行!编译运行时出现
wait no.1connect
Exception in thread "main" java.lang.NullPointerException
at Jsever.main(J.java:47)
----------------解决方案--------------------------------------------------------
问多个问题:
我在学多线程的多人通讯的东东!
import java.net.*;
import java.io.*;
public class Jsever extends Thread{
private Socket sock;
private int id;
DataOutputStream dout;
DataInputStream din;
String ss;
char c;
public Jsever(Socket s,int i){sock=s;id=i;}
public void run()
{
try{
System.out.println("socket.."+id+"..connection success");
dout=new DataOutputStream(sock.getOutputStream());
dout.writeUTF("sock.."+id+"..welcome client");
while(true)
{
din=new DataInputStream(sock.getInputStream());
String s=din.readUTF();
System.out.println("sock.."+id+"..recvice.."+s);
}
} catch(Exception e){System.out.println("err1");}
}
public static void main(String d[])
{
int n=1;
ServerSocket sever=null;
try{
sever=new ServerSocket(6000);
System.out.println("sever start");
}
catch(IOException r){}
while(true){
try{
System.out.println("wait no."+n+"connect");
Socket s=sever.accept();
Jsever t=new Jsever(s,n++);
t.start();
}catch(IOException ee){}
}}
}
//////////////////////////////////client/////////////////
import java.net.*;
import java.io.*;
public class Jclient{
public static void main(String args[])
{try{
if(args.length<2)
return;
Socket s=new Socket(args[0],6000);
DataInputStream in=new DataInputStream(s.getInputStream());
System.out.println(in.readUTF());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeUTF("client"+args[1]+" come");
String ss;char c;
while(true){
ss="";
do{
c=(char)(System.in.read());
if(c!='\n'&&c!='\r')
ss+=c;}while(c!='\n');
dout.writeUTF(ss);
}
}
catch(IOException e){}
}
}
问题是:我想用服务端向客户端发信息.用我这样的程序能实现吗??
----------------解决方案--------------------------------------------------------
空指针啊,你找到那一行看一下是否赋值了。
----------------解决方案--------------------------------------------------------
这样的一段程序我在命令提示符中运行正常!为什么在eclipse就出现java.lang.NoClassDefFoundError: Jsever
Exception in thread "main"
我才接触eclipse,不知道eclipse是不是需要什么设置
!
?
!
----------------解决方案--------------------------------------------------------