服务器的
import java.io.*;
import java.net.*;
import java.util.*;public class severTest {
boolean started = false;
ServerSocket ss = null;

List<Client> clients = new ArrayList<Client>();

public static void main(String[] args) {
new severTest().start();
}

public void start() {
try {
ss = new ServerSocket(8888);
System.out.println("服务器已启动");
started = true;
} 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("一个客户端连接");
new Thread(c).start();
clients.add(c);
}
} 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);
}
}

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);
}
}
} catch (EOFException e) {
System.out.println("客户端已退出");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(dis != null) dis.close();
if(dos != null) dos.close();
if(s != null)  {
s.close();
}

} catch (IOException e1) {
e1.printStackTrace();
}


}
}

}
}
客户端的
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class CliceTest extends Shell { private Text tFa;
private Text tShou;
private Socket socket;
private boolean bConnected = false;
DataOutputStream dos = null;
DataInputStream dis = null;
public static void main(String args[]) {
try {
Display display = Display.getDefault();
CliceTest shell = new CliceTest(display, SWT.SHELL_TRIM);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
} catch (Exception e) {
e.printStackTrace();
}
} public CliceTest(Display display, int style) {
super(display, style);
createContents();
addDisposeListener(new DisposeListener() {
public void widgetDisposed(final DisposeEvent e) {
disconnect();
dispose();
}
});
} /**
 * Create contents of the window
 */
protected void createContents() {
setText("客户端");
setSize(500, 375); tShou = new Text(this, SWT.BORDER);
tShou.setBounds(0, 0, 273, 149); tFa = new Text(this, SWT.MULTI | SWT.BORDER);
tFa.setBounds(0, 187, 273, 116); final Button button = new Button(this, SWT.NONE);
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
new MyServerWriter(dos,tFa).start();
}
});
button.setText("button");
button.setBounds(0, 319, 48, 22);

socket = ClinetCon();

RecvThread rt = new RecvThread();
Thread thread = new Thread(rt);
thread.start();
System.out.println("aaa");

} @Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}  private class RecvThread implements Runnable {
// private Text text;
// private Display disp;
// private String info;
// public RecvThread(Text text,Display disp){
// this.text = text;
// this.disp = disp;
//  }
// public void run() {
// while(bConnected){
// try {
// info = dis.readUTF();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// System.out.println(info);
// disp.asyncExec(new Runnable(){
//
// @Override
// public void run() {
// // TODO Auto-generated method stub
// text.append(info);
// }
//
// });
// }
// }
 public void run() {
try {
while(bConnected = false) {
String str = dis.readUTF();
System.out.println(str);
// taContent.setText(taContent.getText() + str + '\n');
}
} catch (SocketException e) {
System.out.println("退出了,bye!");
} catch (EOFException e) {
System.out.println("推出了,bye - bye!");
} catch (IOException e) {
e.printStackTrace();


}

} private Socket ClinetCon()
{
try {
socket = new Socket("127.0.0.1",8888);
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
}catch (IOException e) {
System.out.println("连接服务器失败!");
}
return socket;
}
public void disconnect() {
try {
dos.close();
dis.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}

}class MyServerWriter extends Thread
{
private DataOutputStream dos;
private Text text;
public MyServerWriter(DataOutputStream dos,Text text)
{
this.dos = dos;
this.text = text;
}

@Override
public void run() {
// TODO Auto-generated method stub
Display.getDefault().asyncExec(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
String info;
try
{
info = text.getText();
dos.writeUTF(info); }
catch(IOException e)
{}
}

});
}
}
}

解决方案 »

  1.   

    没法帮你调
    import org.eclipse.swt.SWT; 
    import org.eclipse.swt.events.DisposeEvent; 
    import org.eclipse.swt.events.DisposeListener; 
    import org.eclipse.swt.events.SelectionAdapter; 
    import org.eclipse.swt.events.SelectionEvent; 
    import org.eclipse.swt.widgets.Button; 
    import org.eclipse.swt.widgets.Display; 
    import org.eclipse.swt.widgets.Shell; 
    import org.eclipse.swt.widgets.Text; 
      

  2.   

    服务器写给客户端的数据之后,要再调用flush()方法
    具体能否运行,我没有调试,楼主可以试下。