java socket 长连接代码实现

服务器端程序:

import java.io.*;
import java.net.*;
import java.util.*;

public class ChatServer {
	boolean started = false;
	ServerSocket ss = null; List<Client> clients = new ArrayList<Client>(); public static void main(String[] args) { new ChatServer().start(); } public void start() { try { ss = new ServerSocket(8888); started = true; System.out.println("端口已开启,占用8888端口号...."); } catch (BindException e) { System.out.println("端口使用中...."); System.out.println("请关掉相关程序并重新运行服务器!"); System.exit(0); } catch (IOException e) { e.printStackTrace(); } try { while (started) { Socket s = ss.accept(); Client c = new Client(s); System.out.println("a client connected!"); new Thread(c).start(); clients.add(c); } } catch (IOException e) { e.printStackTrace(); } finally { try { ss.close(); } catch (IOException e) { e.printStackTrace(); } } } class Client implements Runnable { private Socket s; private DataInputStream dis = null; private DataOutputStream dos = null; private boolean bConnected = false; public Client(Socket s) { this.s = s; try { dis = new DataInputStream(s.getInputStream()); dos = new DataOutputStream(s.getOutputStream()); bConnected = true; } catch (IOException e) { e.printStackTrace(); } } public void send(String str) { try { dos.writeUTF(str); } catch (IOException e) { clients.remove(this); System.out.println("对方退出了!我从List里面去掉了!"); } } public void run() { try { while (bConnected) { String str = dis.readUTF(); System.out.println("------------来自本地服务器:" + str); for (int i = 0; i < clients.size(); i++) { Client c = clients.get(i); c.send(str); } } } catch (EOFException e) { System.out.println("Client closed!"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (dis != null) dis.close(); if (dos != null) dos.close(); if (s != null) { s.close(); } } catch (IOException e1) { e1.printStackTrace(); } } } } }public class ChatServer {
	boolean started = false;
	ServerSocket ss = null;
	List<Client> clients = new ArrayList<Client>(); public static void main(String[] args) { new ChatServer().start(); } public void start() { try { ss = new ServerSocket(8888); started = true; System.out.println("端口已开启,占用8888端口号...."); } catch (BindException e) { System.out.println("端口使用中...."); System.out.println("请关掉相关程序并重新运行服务器!"); System.exit(0); } catch (IOException e) { e.printStackTrace(); } try { while (started) { Socket s = ss.accept(); Client c = new Client(s); System.out.println("a client connected!"); new Thread(c).start(); clients.add(c); } } catch (IOException e) { e.printStackTrace(); } finally { try { ss.close(); } catch (IOException e) { e.printStackTrace(); } } } class Client implements Runnable { private Socket s; private DataInputStream dis = null; private DataOutputStream dos = null; private boolean bConnected = false; public Client(Socket s) { this.s = s; try { dis = new DataInputStream(s.getInputStream()); dos = new DataOutputStream(s.getOutputStream()); bConnected = true; } catch (IOException e) { e.printStackTrace(); } } public void send(String str) { try { dos.writeUTF(str); } catch (IOException e) { clients.remove(this); System.out.println("对方退出了!我从List里面去掉了!"); } } public void run() { try { while (bConnected) { String str = dis.readUTF(); System.out.println("------------来自本地服务器:" + str); for (int i = 0; i < clients.size(); i++) { Client c = clients.get(i); c.send(str); } } } catch (EOFException e) { System.out.println("Client closed!"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (dis != null) dis.close(); if (dos != null) dos.close(); if (s != null) { s.close(); } } catch (IOException e1) { e1.printStackTrace(); } } } } }

客户端程序:

<span style="color:#0000ff">import java.awt.*;
<span style="color:#0000ff">import java.awt.event.*;
<span style="color:#0000ff">import java.io.*;
<span style="color:#0000ff">import java.net.*;

<span style="color:#008000">/**
 * <span style="color:#808080">@author Michael Huang
 * 
 */
<span style="color:#0000ff">public <span style="color:#0000ff">class <span style="color:#a31515">ChatClient <span style="color:#0000ff">extends <span style="color:#a31515">Frame {
	Socket s = <span style="color:#0000ff">null;
	DataOutputStream dos = <span style="color:#0000ff">null;
	DataInputStream dis = <span style="color:#0000ff">null;
	<span style="color:#0000ff">private <span style="color:#0000ff">boolean bConnected = <span style="color:#0000ff">false;

	TextField tfTxt = <span style="color:#0000ff">new TextField();
	TextArea taContent = <span style="color:#0000ff">new TextArea();

	Thread tRecv = <span style="color:#0000ff">new Thread(<span style="color:#0000ff">new RecvThread());

	<span style="color:#0000ff">public <span style="color:#0000ff">static <span style="color:#0000ff">void <span style="color:#a31515">main(String[] args) {
		<span style="color:#0000ff">new ChatClient().launchFrame(8888);
	}

	<span style="color:#0000ff">public <span style="color:#0000ff">void <span style="color:#a31515">launchFrame(<span style="color:#0000ff">int port) {
		setLocation(400, 300);
		<span style="color:#0000ff">this.setSize(300, 300);
		add(tfTxt, BorderLayout.SOUTH);
		add(taContent, BorderLayout.NORTH);
		pack();
		<span style="color:#0000ff">this.addWindowListener(<span style="color:#0000ff">new WindowAdapter() {

			<span style="color:#2b91af">@Override
			<span style="color:#0000ff">public <span style="color:#0000ff">void <span style="color:#a31515">windowClosing(WindowEvent arg0) {
				disconnect();
				System.exit(0);
			}

		});
		tfTxt.addActionListener(<span style="color:#0000ff">new TFListener());
		setVisible(<span style="color:#0000ff">true);
		connect(port);

		tRecv.start();
	}

	<span style="color:#0000ff">public <span style="color:#0000ff">void <span style="color:#a31515">connect(<span style="color:#0000ff">int port) {
		<span style="color:#0000ff">try {
			s = <span style="color:#0000ff">new Socket(<span style="color:#a31515">"127.0.0.1", port);
			dos = <span style="color:#0000ff">new DataOutputStream(s.getOutputStream());
			dis = <span style="color:#0000ff">new DataInputStream(s.getInputStream());
			System.out.println(<span style="color:#a31515">"~~~~~~~~连接成功~~~~~~~~!");
			bConnected = <span style="color:#0000ff">true;
		} <span style="color:#0000ff">catch (UnknownHostException e) {
			e.printStackTrace();
		} <span style="color:#0000ff">catch (IOException e) {
			e.printStackTrace();
		}

	}

	<span style="color:#0000ff">public <span style="color:#0000ff">void <span style="color:#a31515">disconnect() {
		<span style="color:#0000ff">try {
			dos.close();
			dis.close();
			s.close();
		} <span style="color:#0000ff">catch (IOException e) {
			e.printStackTrace();
		}

	}

	<span style="color:#0000ff">private <span style="color:#0000ff">class <span style="color:#a31515">TFListener <span style="color:#0000ff">implements <span style="color:#a31515">ActionListener {

		<span style="color:#0000ff">public <span style="color:#0000ff">void <span style="color:#a31515">actionPerformed(ActionEvent e) {
			String str = tfTxt.getText().trim();
			tfTxt.setText(<span style="color:#a31515">"");

			<span style="color:#0000ff">try {
				dos.writeUTF(str);
				dos.flush();
			} <span style="color:#0000ff">catch (IOException e1) {
				e1.printStackTrace();
			}

		}

	}

	<span style="color:#0000ff">private <span style="color:#0000ff">class <span style="color:#a31515">RecvThread <span style="color:#0000ff">implements <span style="color:#a31515">Runnable {

		<span style="color:#0000ff">public <span style="color:#0000ff">void <span style="color:#a31515">run() {
			<span style="color:#0000ff">try {
				<span style="color:#0000ff">while (bConnected) {
					String str = dis.readUTF();
					taContent.setText(taContent.getText() + str + <span style="color:#a31515">'\n');
				}
			} <span style="color:#0000ff">catch (SocketException e) {
				System.out.println(<span style="color:#a31515">"退出了,bye!");
			} <span style="color:#0000ff">catch (EOFException e) {
				System.out.println(<span style="color:#a31515">"退出了,bye!");
			} <span style="color:#0000ff">catch (IOException e) {
				e.printStackTrace();
			}

		}

	}
}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

猜你喜欢

转载自blog.csdn.net/W_DongQiang/article/details/79838379
今日推荐