兄弟们帮我看看到底怎么回事 我总是弄不好啊
package net;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class MyClient extends JFrame implements ActionListener {
private JTextField f1;
private JTextArea a1;
private JScrollPane s1;
private PrintWriter out;
private BufferedReader in;
private Socket socket;
public MyClient() {
createView();
try {
socket = new Socket("localhost", 8888);
out = new PrintWriter(socket.getOutputStream());
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
new Thread(new Runnable() {
public void run() {
receive();
}
}).start();
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean send(String str) {
out.print(str);
out.flush();
return true;
}
public void receive() {
System.out.println("===== Clinte receive Begin =====");
String Message = null;
while (true) {
try {
Message = in.readLine();
a1.append(Message+"\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void createView() {
f1 = new JTextField(30);
a1 = new JTextArea(35, 35);
s1 = new JScrollPane(a1);
getContentPane().add(f1, "North");
getContentPane().add(s1, "Center");
setSize(300, 300);
setVisible(true);
f1.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent arg0) {
String s = f1.getText();
a1.append("clinet:" + s + "\n");
f1.setText("");
send(s + "\n");
}
public static void main(String[] args) {
new MyClient();
}
}
=====================
package net;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Vector;
public class MyServer {
private Socket socket;
private BufferedReader read;
private PrintWriter writer;
private LinkedList<Socket> list = new LinkedList<Socket>();
public MyServer(){
ServerSocket ssocket = null;
try {
ssocket = new ServerSocket(8888);
} catch (IOException e2) {
e2.printStackTrace();
}
while(true){
try {
socket = ssocket.accept();
list.add(socket);
new Thread(new Runnable(){
public void run() {
try {
receive();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public void receive() throws IOException{
while(true){
read = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String Message = read.readLine();
System.out.println("====Server Recevie======"+Message);
send(Message);
}
}
private void send(String Message){
for(int i=0;i<list.size();i++){
try {
Socket sendSocket = (Socket) list.get(i);
writer = new PrintWriter(sendSocket.getOutputStream());
writer.print(new String("ServerSpeak:"+Message+"\n"));
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
MyServer server = new MyServer();
}
}