Java学习笔记(一)------服务器&客户端一对一通信小程序实现(TCP)

服务器端程序:

package org.ning.study.swing;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Server extends JFrame implements Runnable,ActionListener{
	private ServerSocket serverSocket = null;
	private Socket clientSocket = null;
	String str = null;
	DataOutputStream out = null;
	DataInputStream in = null;
	JButton send = null;
	JTextArea textArea = null;
	JTextField textField = null;
	Thread thread;
	int serverPort = 4331;
	public Server(){
		init();
	}

	public void init(){
		//初始化线程
		thread = new Thread(this);
		//初始化Swing
		Container container = this.getContentPane();
		this.setTitle("服务器端程序");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel panel = new JPanel();
		JLabel label = new JLabel("服务器发送的信息:");
		textField = new JTextField(30);
		 send = new JButton("发送");
		send.addActionListener(this);
		panel.setLayout(new FlowLayout(FlowLayout.LEFT));
		panel.add(label);
		panel.add(textField);
		panel.add(send);
		container.setLayout(new BorderLayout());
		container.add(panel,BorderLayout.NORTH);
		textArea = new JTextArea();
		container.add(textArea,BorderLayout.CENTER);
		this.setBounds(500, 300, 700, 400);
		this.validate();
		this.setVisible(true);
		
		//初始化socket
		try {
			textArea.append("正在端口" + serverPort + "上创建套接字\n");
			serverSocket = new ServerSocket(serverPort);
			textArea.append("套接字创建成功,等待客户端连接...\n");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		thread.start();//启动线程
		
	}
		
	
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == send){
			try {
				out.writeUTF(textField.getText());//点击发送就发送一条信息
				textArea.append("服务器说:" + textField.getText() + "\n");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		
	}
	
	
	//该线程每1秒扫描一次端口,以便接收来自客户端的信息
	@Override
	public void run() {
		try {
			clientSocket = serverSocket.accept();// 客户端如果不链接将会阻塞在这里
			textArea.append("客户端已连接 , " + clientSocket.getInetAddress().getHostAddress() + ":" + clientSocket.getPort() + "\n"  );
			
			
			in = new DataInputStream(clientSocket.getInputStream());
			out = new DataOutputStream(clientSocket.getOutputStream());
			while (true) {
				str = in.readUTF();
				//out.writeUTF(str);
				textArea.append("客户端说:"  + str + "\n");
				thread.sleep(1000);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	public static void main(String[] args) {
		Server server = new Server();
		
	}
	
	
}
客户端程序:

package org.ning.study.swing;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Client extends JFrame implements Runnable, ActionListener {
	ServerSocket serverSocket = null;
	Socket clientSocket;
	String str = null;
	DataOutputStream out = null;
	DataInputStream in = null;
	JButton send = null;
	JTextArea textArea = null;
	JTextField textField = null;
	Thread thread;
	int serverPort = 4332;
	public Client(){
		init();
	}
	
	public void init() {
		// 初始化线程
		thread = new Thread(this);
		// 初始化Swing
		Container container = this.getContentPane();
		this.setTitle("客户端端程序");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel panel = new JPanel();
		JLabel label = new JLabel("客户端发送的信息:");
		textField = new JTextField(30);
		send = new JButton("发送");
		send.addActionListener(this);
		panel.setLayout(new FlowLayout(FlowLayout.LEFT));
		panel.add(label);
		panel.add(textField);
		panel.add(send);
		container.setLayout(new BorderLayout());
		container.add(panel, BorderLayout.NORTH);
		textArea = new JTextArea();
		container.add(textArea, BorderLayout.CENTER);
		this.setBounds(500, 300, 700, 400);
		this.validate();
		this.setVisible(true);
		//连接远端套接字,并获得输入输出流
		try {
			textArea.append("尝试连接...\n");
			clientSocket = new Socket("127.0.0.1",4331);
			textArea.append("完成连接。" + clientSocket.getInetAddress().getHostAddress() + ":" + clientSocket.getPort() + "\n");
			in = new DataInputStream(clientSocket.getInputStream());
			out = new DataOutputStream(clientSocket.getOutputStream());
		} catch (Exception e) {
			e.printStackTrace();
		}
		thread.start();
	}
	
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getSource() == send){
			try {
				out.writeUTF(textField.getText());
				textArea.append("客户端说:" + textField.getText() + "\n");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	@Override
	public void run() {//不断循环读取服务器发送的消息
		while(true){
			try {
				str = in.readUTF();
				textArea.append("服务器说:" + str + "\n");
				thread.sleep(1000);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
		Client client = new Client();
	}

}



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值