学习了Socket编程,做了一个简单的聊天程序练习,代码如下 :
//服务器端
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.GridLayout;
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 javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class SimpleChat implements ActionListener {
JFrame f=new JFrame("chat测试版服务器端");//容器
JTextArea chatCotent=new JTextArea();//聊天区
JTextField sendCotent=new JTextField(15);//待发送框内容
Button send=new Button("发送");//发送按钮
Thread recieveThread=new Thread(); //控制接收线程
ServerSocket server;
Socket client;
InputStream is;
OutputStream os;
String ip=null;
public static void main(String argv[]) throws IOException{
new SimpleChat();
}
public SimpleChat() throws IOException{
f.setLayout(new BorderLayout());
f.setSize(600, 400);
f.setLocationRelativeTo(null);
JPanel p1=new JPanel();
f.add("Center",chatCotent);
p1.add(sendCotent);
p1.add(send);
f.add("South",p1);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
send.addActionListener(this);//监听发送按钮
sendCotent.addActionListener(this);//监听文本框
server=new ServerSocket(8888);
client=server.accept();
chatCotent.append("客户端"+client.getInetAddress().getHostName()+"已连接\n\n");
ip=client.getInetAddress().getHostName();
os=client.getOutputStream();
is=client.getInputStream();
recieveThread=new Thread(new Myrun());
recieveThread.start();
}
//设置接收
class Myrun implements Runnable{
public void run() {
while(true){
try {
is = client.getInputStream();
byte buff[]=new byte[512];
is.read(buff);
String cotent=new String(buff);
if(sendCotent.getText()!=null)
chatCotent.append(ip+cotent+":\n\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//信息发送
public void actionPerformed(ActionEvent e){
try {
byte buf[]=sendCotent.getText().getBytes();
chatCotent.append(ip+":\n"+sendCotent.getText());
os.write(buf);
chatCotent.append("\n\n");
sendCotent.setText("");
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
//客户端
import java.awt.BorderLayout;
import java.awt.Button;
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.Socket;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class chatClient implements ActionListener {
JFrame f=new JFrame("chat测试版客户端");//容器
JTextArea chatCotent=new JTextArea();//聊天区
JTextField sendCotent=new JTextField(15);//待发送框内容
Button send=new Button("发送");//发送按钮
Socket client;
OutputStream os;
InputStream is;
String ip=null;
public chatClient() throws IOException{
f.setLayout(new BorderLayout());
f.setSize(600, 400);
f.setLocationRelativeTo(null);
JPanel p1=new JPanel();
f.add("Center",chatCotent);
p1.add(sendCotent);
p1.add(send);
f.add("South",p1);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
send.addActionListener(this);
sendCotent.addActionListener(this);
Socket client=new Socket("192.168.1.105",8888);
chatCotent.append("已经和服务器连接:"+client.getInetAddress().getHostName()+"\n\n");
ip=client.getInetAddress().getHostName();
is=client.getInputStream();
os=client.getOutputStream();
Thread recieveThread=new Thread(new Myrun());
recieveThread.start();
}
public static void main(String[] args) throws IOException {
new chatClient();
}
//接受信息
class Myrun implements Runnable{
public void run() {
while(true){
try {
byte buff[]=new byte[512];
is.read(buff);
String cotent=new String(buff);
if(sendCotent.getText()!=null)
chatCotent.append(ip+cotent+":\n\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//信息发送
public void actionPerformed(ActionEvent e) {
try {
byte buf[]=sendCotent.getText().getBytes();
chatCotent.append(ip+":\n"+sendCotent.getText());
os.write(buf);
chatCotent.append("\n\n");
sendCotent.setText("");
} catch (IOException e1) {
e1.printStackTrace();
}
}
}