菜鸟 求解决java的tcp问题
SERVER端代码:
package Server;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import
java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Server {
public static void main(String[] args) {
ChatServerFrame f = new ChatServerFrame();
}
}
class ChatServerFrame extends JFrame{
ChatServerPanel serverPanel ;
public ChatServerFrame(){
serverPanel = new ChatServerPanel();
this.add(serverPanel);
this.setTitle("///的C/S server端");
this.setSize(500,500);
this.setVisible(true);
}
}
class ChatServerPanel extends JPanel{
ArrayList clientsList =new ArrayList();
ServerSocket server;
JTextArea textarea ;
JButton start ;
JButton quit;
JPanel buttonpanel ;
public ChatServerPanel(){
buttonpanel = new JPanel();
this.setLayout(new BorderLayout());textarea = new JTextArea(300,300);
quit=new JButton("退出");
start = new JButton("启动");
buttonpanel.add(quit);buttonpanel.add(start);
quit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try {
startServer();
} catch (
IOException e1) {
e1.printStackTrace();
}
}
});
this.add(textarea,BorderLayout.CENTER);
this.add(buttonpanel,BorderLayout.NORTH);
}
public void setText(String text){
textarea.append(text+"\r\n");}
//这个方法是用来启动服务器,按钮start(启动)就会调用这个方法
public void startServer() throws IOException{
if(server!=null){
setText(" 服务器已经启动于端口9999,不需要再次启动");
return ;
}
server = new ServerSocket(9999);
Thread s = new ServerAcceptThread(server,this);
s.start();//启动监听客户连接的线程
setText("服务器已经成功启动,使用端口9999");}
}
/**
* 线程1:监听客户端的线程,当有客户端来连接时,把接收到的Socket对象放到列表中
* */
class ServerAcceptThread extends Thread{
ChatServerPanel p ;
ServerSocket server ;
int num=1;
Date d = new Date();
ServerAcceptThread(ServerSocket server,ChatServerPanel p){
this.server = server;
this.p = p;
}
public void run(){
while(true){
try {
Socket socket =server.accept();
Thread receiveThread=new ReceiveWordThread(socket,p);
receiveThread.start();
// p.clientsList.add(socket);
String ip = socket.getInetAddress().toString();
int port=socket.getPort();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd kk:mm:ss ");
String date=sdf.format(d);
p.setText("客户端"+num+++"连接上:"+"ip地址是:"+ip+"端口号是:"+port+"连接时间是:"+date);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
*
* 线程2:负责接收客户端发过来的内容
* 有两个作用:1:当有客户端连接时,启动该线程2:通过soket得到InputStream * */
class ReceiveWordThread extends Thread{
Socket s;
ChatServerPanel p;
Date d = new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd kk:mm:ss ");
String date=sdf.format(d);
public ReceiveWordThread(Socket s,ChatServerPanel p) throws IOException{