1.窗口聊天
代码:
服务器:
package MyFrame;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyServer implements ActionListener {
ServerSocket ss;
Socket s;
int port;
String str;
BufferedReader br;
PrintStream p;
JFrame f;
JLabel l1,l2,l3;
JButton b1,b2,b3;
JTextArea ta;
JTextField t1,t2,t3;
public MyServer() {
f=new JFrame("服务器端");
l1=new JLabel("ip地址:");
l2=new JLabel("端口号:");
l3=new JLabel("服务器对话框:");
b1=new JButton("确定");
b2=new JButton("关闭");
b3=new JButton("发送");
t1=new JTextField(20);
t2=new JTextField(20);
t3=new JTextField(20);
ta=new JTextArea();
ta.setLineWrap(true);
ta.setEditable(false);
f.setSize(500, 600);
f.setVisible(true);
f.setLayout(null);
f.add(l1);
f.add(t1);
f.add(l2);
f.add(t2);
f.add(l3);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(ta);
f.add(t3);
l1.setBounds(50, 15, 50, 25);
t1.setBounds(100, 15, 120, 25);
l2.setBounds(250, 15, 50, 25);
t2.setBounds(300, 15, 120, 25);
b1.setBounds(160, 50, 60, 30);
b2.setBounds(250, 50, 60, 30);
ta.setBounds(0, 90, 500, 420);
l3.setBounds(10, 520, 100, 30);
t3.setBounds(100, 520, 300, 30);
b3.setBounds(410, 520, 60, 30);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
f.setBounds(100, 100, 500, 600);
}
public static void main(String[] args) throws IOException {
new MyServer();
}
@Override
public void actionPerformed(ActionEvent e) {
Thread t1=new Thread(new Runnable() {
public void run() {
try {
ss=new ServerSocket(port);
s=ss.accept();
ta.append("客户端连接成功!\n");
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
p=new PrintStream(s.getOutputStream());
p.println("已成功连接服务器!");
while(true) {
str=br.readLine();
ta.append("客户端:"+str+"\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}});
String str1=e.getActionCommand();
if(str1.equals("确定")) {
String str2=t2.getText();
port=Integer.parseInt(str2);
ta.append("服务器启动完成!等待连接中......\n");
t1.start();
}
else if(str1.equals("关闭")) {
System.exit(0);
}
else if(str1.equals("发送")){
String str3=t3.getText();
ta.append(str3+"\n");
p.println(str3);
t3.setText("");
}
}
}
客户端:
package MyFrame;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyClient implements ActionListener {
Socket s;
int port;
String str,ip1;
BufferedReader br;
PrintStream p;
JFrame f;
JLabel l1,l2,l3;
JButton b1,b2,b3;
JTextArea ta;
JTextField t1,t2,t3;
public MyClient() {
f=new JFrame("客户端");
l1=new JLabel("ip地址:");
l2=new JLabel("端口号:");
l3=new JLabel("客户端对话框:");
b1=new JButton("确定");
b2=new JButton("关闭");
b3=new JButton("发送");
t1=new JTextField(20);
t2=new JTextField(20);
t3=new JTextField(20);
ta=new JTextArea();
ta.setLineWrap(true);
ta.setEditable(false);
f.setSize(500, 600);
f.setVisible(true);
f.setLayout(null);
f.add(l1);
f.add(t1);
f.add(l2);
f.add(t2);
f.add(l3);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(ta);
f.add(t3);
l1.setBounds(50, 15, 50, 25);
t1.setBounds(100, 15, 120, 25);
l2.setBounds(250, 15, 50, 25);
t2.setBounds(300, 15, 120, 25);
b1.setBounds(160, 50, 60, 30);
b2.setBounds(250, 50, 60, 30);
ta.setBounds(0, 90, 500, 420);
l3.setBounds(10, 520, 100, 30);
t3.setBounds(100, 520, 300, 30);
b3.setBounds(410, 520, 60, 30);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
f.setBounds(700, 100, 500, 600);
}
public static void main(String[] args) throws IOException {
new MyClient();
}
@Override
public void actionPerformed(ActionEvent e) {
Thread t=new Thread(new Runnable() {
public void run() {
try {
s=new Socket(ip1,port);
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
p=new PrintStream(s.getOutputStream());
while(true){
str=br.readLine();
ta.append("服务器:"+str+"\n");
}
}
catch (UnknownHostException e1) {
ta.append("IP地址或端口号输入错误!");
e1.printStackTrace();
}
catch (ConnectException e2) {
ta.append("找不到对应服务器!端口号输入错误!");
e2.printStackTrace();
}
catch(SocketException e3) {
ta.append("连接错误!");
e3.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
});
String str1=e.getActionCommand();
if(str1.equals("确定")) {
ip1=t1.getText();
String str2=t2.getText();
port=Integer.parseInt(str2);
t.start();
ta.append("客户端启动成功!正在连接中......\n");
}
else if(str1.equals("关闭")) {
System.exit(0);
}
else if(str1.equals("发送")) {
String str5=t3.getText();
ta.append(str5+"\n");
p.println(str5);
t3.setText("");
}
}
}代码:
服务器:
package MyFrame;
import java.