⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientsocketone.java

📁 java 点对点聊天系统 可以用于局域网 同学之间通信
💻 JAVA
字号:
package net.client;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import javax.swing.JOptionPane;

public class ClientSocketOne {

	private Socket so;

	DataOutputStream dos;

	DataInputStream dis;

	BufferedReader br;

	FileReader file;

	Thread t = new Thread(new Thoo());

	String ip;

	int port;
	Client client;
	public ClientSocketOne(Client client) {
		this.client = client;
		readIP();
	}
	
	/** 开启服务 */
	public void starts() {
		/* while(this.ip !=null &&this.port !=0); */
		client();
		t.start();
		// rend("我要测试");
		/* System.out.println("kai shi le wu fu"); */
	}

	/** 读取服务器IP地址和端口号 */
	private void readIP() {
		try {
			file = new FileReader("ip.txt");
			br = new BufferedReader(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null,"系统找不到ip.txt文件!","加载错误",JOptionPane.ERROR_MESSAGE);
			System.exit(0);
		}

		try {
			ip = br.readLine();
			port = Integer.parseInt(br.readLine());
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private void client() {
		try {
			so = new Socket();
			InetSocketAddress add = new InetSocketAddress(ip, port);
			so.connect(add, 1000);// 建立连接超时
			dos = new DataOutputStream(so.getOutputStream());
			dis = new DataInputStream(so.getInputStream());

		} catch (SocketTimeoutException e) {
			/* 如果没有启动服务器,那么程序就不能运行 */
			JOptionPane.showMessageDialog(null, "没有启动服务器或者连接超时,请检查网络连接",
					"服务器错误", JOptionPane.ERROR_MESSAGE);
			System.exit(0);
		} catch (ConnectException e) {
			e.printStackTrace();
		} catch (UnknownHostException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}/*
			 * finally{ state = true; }
			 */

	}

	/**
	 * 返回此客户端连接状态
	 */
	/*
	 * public boolean getState(){ return (state?true:false); }
	 */
	/** 发送指令 */
	public void rend(String ss) {
		try {
			dos.writeUTF(ss);
			dos.flush();

		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/** 关闭连接 */
	public void close() {
		try {
			if (dis != null)
				dis.close();
			if (dos != null)
				dos.close();
			if (so != null)
				so.close();
		} catch (IOException e) {

			e.printStackTrace();
		}

	}

	/** 接收指令 */
	class Thoo implements Runnable {

		public void run() {

			while (true) {
				try {
					String net = dis.readUTF();
					//System.out.println(net);
					client.textArea.append(net+"\n");
				} catch (IOException e) {

					try {
						if (dis != null)
							dis.close();
						if (dos != null)
							dos.close();

						if (so != null)
							so.close();
					} catch (IOException e1) {

						e1.printStackTrace();
					}
				}
			}
		}

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -