目录

TCP程序设计实例
思路:
1、分别编写了MyServer,MyClient的JFrame文件和MyServerReadThread、MyClientReadThread的线程类文件
2、在MyServer,MyClient中建立read()和write()方法,
3、在MyServer中在按钮发送中调用write()方法,在启动服务按钮中开启线程MyServerReadThread,在MyServerReadThread线程中run()方法中调用server.read()方法。
4、在MyClient中进行与MyServer相类似的操作

package com.IM;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.awt.event.ActionEvent;

public class MyServer extends JFrame
private ServerSocket server;
private Socket socket;
private JTextArea textArea;
private boolean isRunning=true;

private JPanel contentPane;

public boolean isRunning() {
return isRunning;
}

public void setRunning(boolean isRunning) {
this.isRunning = isRunning;
}

/**
* Launch the application.
*/
public static void main(String[] args) {


EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MyServer frame = new MyServer();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public MyServer() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 445, 491);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JButton button = new JButton("启动服务");

button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("服务器启动");
try {
server=new ServerSocket(8080);
socket=server.accept();//等待连接,准备开启线程
Thread t=new Thread(new MyServerReadThread(MyServer.this));//不很懂
t.start();
System.out.println("有客户端连接");


} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
});


button.setBounds(10, 10, 93, 23);
contentPane.add(button);

textArea = new JTextArea();
textArea.setBounds(10, 396, 312, 47);
contentPane.add(textArea);

JButton button_1 = new JButton("发送");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

write();//进行写操作
}
});
button_1.setBounds(332, 396, 93, 23);
contentPane.add(button_1);
}
public void read(){
InputStream is;
try {
is = socket.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String line=br.readLine();
System.out.println("服务器接收到信息"+"\n"+line);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void write(){
try {
OutputStream os=socket.getOutputStream();
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os));
String words=textArea.getText();
System.out.println("客户端说:"+"\n"+words);
bw.write(words+"\n");
textArea.setText("");//注意写完之后清空
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
package com.IM;

public class MyServerReadThread implements Runnable{
private MyServer server;
//在构造器中传参数
public MyServerReadThread (MyServer server){
this.server=server;

}

@Override
public void run() {
while(true){
server.read();
}
}
public MyServer getServer() {
return server;
}

public void setServer(MyServer server) {
this.server = server;
}


}
package com.IM;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.DefaultButtonModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.awt.event.ActionEvent;

public class MyClient extends JFrame
private Socket socket;
private JTextArea textArea;
private JPanel contentPane;
private JList list;
private DefaultListModel<String> modle;


/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MyClient frame = new MyClient();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public MyClient() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 484, 442);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

textArea = new JTextArea();
textArea.setBounds(10, 299, 332, 78);
contentPane.add(textArea);

JButton btnNewButton = new JButton("发送");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
write();
}
});
btnNewButton.setBounds(352, 303, 106, 60);
contentPane.add(btnNewButton);

list = new JList();
list.setBounds(10, 22, 332, 268);
modle=new DefaultListModel<>();
list.setModel(modle);
contentPane.add(list);

JButton btnNewButton_1 = new JButton("连接服务器");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
System.out.println("连接");
socket=new Socket("192.168.0.35", 8080);
Thread t=new Thread(new MyClientReadThread(MyClient.this));
t.start();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
});
btnNewButton_1.setBounds(352, 40, 106, 67);
contentPane.add(btnNewButton_1);
}

public void read(){
try {
InputStream is = socket.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String line=br.readLine();
modle.addElement("服务器说的"+line); //出错
System.out.println("服务器说的"+line);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void write(){
try {
OutputStream os=socket.getOutputStream();
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os));
String words=textArea.getText();
modle.addElement("客户说: "+words);//
System.out.println("客户端说"+words);
bw.write(words);
textArea.setText("");
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
package com.IM;

public class MyClientReadThread implements Runnable{
private MyClient client;
public MyClientReadThread(MyClient client) {
this.client=client;
}
@Override
public void run() {
while(){
client.read();
}
}

}

TCP 程序设计(即时通讯)_.net