📄 tcpserver.txt
字号:
服务器代码
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.util.List;
import java.util.ArrayList;
public class TCPServer extends JFrame
{
private ServerSocket ss = null;
private boolean bStart = false;
private JTextArea taContent = new JTextArea();
private int index = 0;
List<Client> clients = new ArrayList<Client>();
public void launchFrame()
{
Container con = this.getContentPane();
taContent.setEditable(false);
taContent.setBackground(Color.DARK_GRAY);
taContent.setForeground(Color.YELLOW);
this.add(taContent);
this.setSize(300,350);
this.setLocation(400,200);
this.setTitle("TCP Server");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tcpMonitor();
}
public void tcpMonitor()
{
try {
ss = new ServerSocket(8888);
bStart = true;
} catch (IOException e) {
e.printStackTrace();
}
try
{
while(bStart)
{
index ++;
Socket s = ss.accept();
Client c = new Client(s);
clients.add(c);
taContent.append(s.getInetAddress().getHostAddress()+" connected "+index+" clients\n");
new Thread(c).start();
}
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String args[])
{
TCPServer ts = new TCPServer();
ts.launchFrame();
}
private class Client implements Runnable
{
DataInputStream dis = null;
DataOutputStream dos = null;
Socket s = null;
boolean bStart = false;
Client(Socket s)
{
this.s = s;
try
{
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
}
catch(IOException e)
{
e.printStackTrace();
}
bStart = true;
}
public void sendToEveryClient(String str)
{
try
{
dos.writeUTF(str);
dos.flush();
}
catch(IOException e)
{
index--;
clients.remove(this);
taContent.append(s.getInetAddress().getHostAddress()+" exited "+index+" clients\n");
System.out.println("对方退出了!我从List里面去掉了!");
}
}
public void run() {
// TODO Auto-generated method stub
try
{
while(bStart)
{
String str = dis.readUTF();
System.out.println(str);
for(int i=0;i<clients.size();i++)
{
Client c = clients.get(i);
c.sendToEveryClient(str);
}
}
}
catch(EOFException e)
{
clients.remove(this);
taContent.append(s.getInetAddress().getHostAddress()+" exited "+clients.size()+" clients\n");
System.out.println("client closed");
}
catch(SocketException e)
{
System.out.println("client closed");
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if(s != null) s.close();
if(dis != null) dis.close();
if(dos != null) dos.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}
}
客户端程序代码
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.net.*;
public class TCPClient extends JFrame
{
TextArea taContent = new TextArea();
JTextField tfTxt = new JTextField(20);
JButton send = new JButton("发送");
JButton connect = new JButton("连接");
JButton clear = new JButton("清空");
boolean live = false;
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
Socket s = null;
DataOutputStream dos = null;
DataInputStream dis = null;
boolean bConnected = false;
Thread t = new Thread(new RecToServer());
public void launchFrame()
{
taContent.setEditable(false);
p2.setLayout(new FlowLayout(FlowLayout.CENTER,10,5));
p2.add(send);
p2.add(connect);
p2.add(clear);
Container con = this.getContentPane();
con.add(taContent,"North");
con.add(tfTxt,"Center");
con.add(p2,"South");
this.setSize(300,350);
this.setLocation(400,200);
this.setTitle("Chat Client");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
connect.addActionListener(new Connect());
send.addActionListener(new SendMsg());
clear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
taContent.setText("");
}
});
}
public void connectToServer()
{
try {
s = new Socket("127.0.0.1",8888);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
bConnected = true;
}
catch(BindException e)
{
System.out.println("找不到指定的服务器");
}
catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void disConnect()
{
try
{
if(s != null)
{
s.close();
}
if(dos != null)
{
dos.close();
}
if(dis != null)
{
dis.close();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static void main(String args[])
{
TCPClient tc = new TCPClient();
tc.launchFrame();
}
private class Connect implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="连接")
{
connectToServer();
try
{
t.start();
}
catch(IllegalThreadStateException ex)
{
}
connect.setText("断开连接");
}
else if(e.getActionCommand()=="断开连接")
{
disConnect();
connect.setText("连接");
}
}
}
private class SendMsg implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(connect.getActionCommand()=="连接")
{
JOptionPane.showMessageDialog(TCPClient.this,"没有找到指定的服务器","错误提示",1);
}
else
{
String str = tfTxt.getText();
tfTxt.setText("");
try
{
dos.writeUTF(str);
dos.flush();
}
catch(SocketException ex)
{
System.out.println("没有找到指定的服务器");
JOptionPane.showMessageDialog(TCPClient.this,"没有找到指定的服务器","错误提示",1);
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -