import java.io.*;
import java.net.*;public class ChatClient{
Socket s = null;
static DataOutputStream dos = null;
static DataInputStream dis = null;
//发送的字符串
public String ostr = null;
//返回的字符串
public String istr = null;
public boolean bConnected = false; Thread tRecv = new Thread(new RecvThread()); public static void main(String[] args) {
new ChatClient().launchs();
} public void launchs() {
connect();
tRecv.start();
} public void connect() {
try {
s = new Socket("127.0.0.1", 8888);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
System.out.println("connected!");
bConnected = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} } public void disconnect() {
try {
dos.close();
dis.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
} /*
try {
bConnected = false;
tRecv.join();
} catch(InterruptedException e) {
e.printStackTrace();
} finally {
try {
dos.close();
dis.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
*/
}
public void sendMsg(){
try {
//客户端发送信息
System.out.println(ostr);
dos.writeUTF(ostr);
dos.flush();
//dos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
} private class RecvThread implements Runnable { public void run() {
try {
while (bConnected) {
//客户端接收信息
String str = dis.readUTF();
System.out.println("客户端接收信息str="+str);
//new ChatClient().setIstr(str);
istr = str;
//new ChatClient().setIstr(istr);
}
} catch (SocketException e) {
System.out.println("退出了,bye!");
} catch (EOFException e) {
System.out.println("�˳��ˣ�bye 退出了,bye - bye!");
} catch (IOException e) {
e.printStackTrace();
}
} }
class Struct implements java.io.Serializable
{
Byte a[]=new Byte[5];
int b;
}
//发送的字符串
public String getOstr() {
return ostr;
}
//发送的字符串
public void setOstr(String ostr) {
this.ostr = ostr;
}
//返回的字符串
public String getIstr() {
return istr;
}
//返回的字符串
public void setIstr(String istr) {
this.istr = istr;
} public Socket getS() {
return s;
} public void setS(Socket s) {
this.s = s;
} public DataOutputStream getDos() {
return dos;
} public void setDos(DataOutputStream dos) {
ChatClient.dos = dos;
} public DataInputStream getDis() {
return dis;
} public void setDis(DataInputStream dis) {
ChatClient.dis = dis;
} public boolean isBConnected() {
return bConnected;
} public void setBConnected(boolean connected) {
bConnected = connected;
} public Thread getTRecv() {
return tRecv;
} public void setTRecv(Thread recv) {
tRecv = recv;
}
}
应为每笔数据都要通过socket和服务器端联系所以想单独写出来好在任何地方都能调用
在另一个类里面这样调用
ChatClient cc = new ChatClient();
String ostr = "send"+deptName+deptCode+deptCzzh+deptZjm;
cc.launchs();
cc.setOstr(ostr);
cc.sendMsg();
//ostr客户端发送的数据,istr客户端接收的数据
System.out.println("ostr="+cc.getOstr()+"istr="+cc.getIstr()+"\n");
//cc.disconnect();
打印出的结果是connected!
sendjhjh
ostr=sendjhjhistr=null客户端接收信息str=sendjhjh在这里cc.getIstr()得不到值啊。
而在ChatClient.java
里是有这个值的。。
不知道我说清楚没?????
c.getIstr()可能是 null ,也可能不是,如果你要这做的话,需要在这个程序放到另一个thread,并join()
服务器端代码import java.io.*;
import java.net.*;
import java.util.*;public class ChatServer {
//服务端启动标志
boolean started = false;
ServerSocket ss = null; List<Client> clients = new ArrayList<Client>(); public static void main(String[] args) {
new ChatServer().start();
} public void start() {
try {
ss = new ServerSocket(8888);
started = true;
System.out.println("服务启动......");
} catch (BindException e) {
System.out.println("端口使用中....");
System.out.println("请关掉相关程序并重新运行服务器!");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
} try { while (started) {
Socket s = ss.accept();
Client c = new Client(s);
System.out.println("a client connected!");
new Thread(c).start();
clients.add(c);
// dis.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} class Client implements Runnable {
private Socket s;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean bConnected = false; public Client(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
bConnected = true;
} catch (IOException e) {
e.printStackTrace();
}
} public void send(String str) {
try {
dos.writeUTF(str);
} catch (IOException e) {
clients.remove(this);
System.out.println("对方退出了!我从List里面去掉了!");
// e.printStackTrace();
}
} public void run() {
try {
while (bConnected) {
String str = dis.readUTF();
System.out.println(str);
for (int i = 0; i < clients.size(); i++) {
Client c = clients.get(i);
c.send(str);
System.out.println(" a string send !");
}
/*
* for(Iterator <Client> it = clients.iterator();
* it.hasNext(); ) { Client c = it.next(); c.send(str); }
*/
/*
* Iterator <Client> it = clients.iterator();
* while(it.hasNext()) { Client c = it.next(); c.send(str); }
*/
}
} catch (EOFException e) {
System.out.println("Client closed!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (dis != null)
dis.close();
if (dos != null)
dos.close();
if (s != null) {
s.close();
// s = null;
} } catch (IOException e1) {
e1.printStackTrace();
} }
} }
}
放到另外一个线程中吧,不过很样的话,你要等cc接收完所有的内容,
import java.net.*;public class ChatClient{
Socket s = null;
static DataOutputStream dos = null;
static DataInputStream dis = null;
//发送的字符串
public String ostr = null;
//返回的字符串
public String istr = null;
public boolean bConnected = false; Thread tRecv = new Thread(new RecvThread());
public ChatClient(String str){
this.ostr=str;
tRecv.start();
connect();
} public void connect() {
try {
s = new Socket("127.0.0.1", 8888);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
System.out.println("connected!");
bConnected = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} } public void disconnect() {
try {
dos.close();
dis.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
} /*
try {
bConnected = false;
tRecv.join();
} catch(InterruptedException e) {
e.printStackTrace();
} finally {
try {
dos.close();
dis.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
*/
}
public void sendMsg(){
try {
//客户端发送信息
System.out.println(ostr);
dos.writeUTF(ostr);
dos.flush();
//dos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
public String receiveMessage() throws IOException {
String message = null;
try{
message = dis.readUTF();
//dis.close();
}catch(Exception ex){
ex.printStackTrace();
}
return message;
}
private class RecvThread implements Runnable { public void run() {
try {
while (bConnected) {
//客户端接收信息
String str = dis.readUTF();
System.out.println("客户端接收信息str="+str);
//new ChatClient().setIstr(str);
istr = str;
//new ChatClient().setIstr(istr);
}
} catch (SocketException e) {
System.out.println("退出了,bye!");
} catch (EOFException e) {
System.out.println("�˳��ˣ�bye 退出了,bye - bye!");
} catch (IOException e) {
e.printStackTrace();
}
} }
class Struct implements java.io.Serializable
{
Byte a[]=new Byte[5];
int b;
}
//发送的字符串
public String getOstr() {
return ostr;
}
//发送的字符串
public void setOstr(String ostr) {
this.ostr = ostr;
}
//返回的字符串
public String getIstr() {
return istr;
}
//返回的字符串
public void setIstr(String istr) {
this.istr = istr;
} public Socket getS() {
return s;
} public void setS(Socket s) {
this.s = s;
} public DataOutputStream getDos() {
return dos;
} public void setDos(DataOutputStream dos) {
ChatClient.dos = dos;
} public DataInputStream getDis() {
return dis;
} public void setDis(DataInputStream dis) {
ChatClient.dis = dis;
} public boolean isBConnected() {
return bConnected;
} public void setBConnected(boolean connected) {
bConnected = connected;
} public Thread getTRecv() {
return tRecv;
} public void setTRecv(Thread recv) {
tRecv = recv;
}
}
http://hi.baidu.com/magiccode/blog/item/09deb60f0eaea9edab6457a2.html这个分析得比较透,而且讲到了你的方法为什么会为nullhttp://mgc.ahau.edu.cn/article.asp?id=231
这个只是讲下join,不过就是告诉你怎么用,PS:楼主的话,好伤人心.........