Java-TCP程序编写(TCP客户端和服务端)

1.TCP服务端的程序编写

package test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class MyTcp{
	private BufferedReader reader;
	private ServerSocket serverSocket;
	private Socket socket;
	/**
	 * 创建服务端的程序,读取客户端传来的数据
	 */
	void getserver(){
		try {
			serverSocket = new ServerSocket(8998);		//实例化服务端socket
			System.out.println("服务器套接字已经创建成功");
			while (true) {
				System.out.println("等待客户机的连接:");
				socket = serverSocket.accept();        //实例化socket对象
				reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));	//实例化BufferReader对象
				getClientMessage();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	private void getClientMessage() {
		try {
			while (true) {
				System.out.println("客户机传来的信息是:"+reader.readLine());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		MyTcp myTcp = new MyTcp();			//创建本类对象
		myTcp.getserver();
	}
}

2.TCP客户端程序编写

package test;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;
import java.net.Socket;
import java.nio.channels.WritableByteChannel;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;

public class MyTcpClient extends JFrame{
	private PrintWriter printWriter;
	Socket socket;
	private JTextField jTextField = new JTextField();
	private JTextArea jTextArea = new JTextArea();
	Container container;
	/**
	 * 创建的Tcp客户端程序
	 */
	
	public MyTcpClient (String title) {
		super(title);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		container = this.getContentPane();
		final JScrollPane jScrollPane = new JScrollPane();
		jScrollPane.setBorder(new BevelBorder(BevelBorder.RAISED));    //显示边框
		getContentPane().add(jScrollPane,BorderLayout.CENTER);
		jScrollPane.setViewportView(jTextArea);
		container.add(jTextField,"South");			//将文本框放在窗体下面 
		jTextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				printWriter.println(jTextField.getText());		//将文本框的信息写入流(为下面的输出流写入信息做准备)
				jTextArea.append(jTextField.getText() + "\n");
				jTextArea.setSelectionEnd(jTextArea.getText().length());
				jTextField.setText(null);
			}
		});
	}
	
	private void connect() {
		jTextArea.append("尝试连接中...\n");
		try {
			socket = new Socket("127.0.0.1",8998);
			printWriter = new PrintWriter(socket.getOutputStream(),true);   //将printwriter中的信息流写入到套接字的输出流传送给服务端
			jTextArea.setText("已完成连接\n\n");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		MyTcpClient myTcpClient = new MyTcpClient("向服务器发送数据");
		myTcpClient.setSize(500,200);
		myTcpClient.setVisible(true);
		myTcpClient.connect();
	}
}


3.效果展示
<1>先将服务端的程序跑起来
Java-TCP程序编写(TCP客户端和服务端)_第1张图片
<2>再将客户端的程序跑起来
Java-TCP程序编写(TCP客户端和服务端)_第2张图片
<3>.客户端和服务端进行交互
Java-TCP程序编写(TCP客户端和服务端)_第3张图片

你可能感兴趣的:(java)