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

📄 clientsend.java

📁 一个简易的java画图软件
💻 JAVA
字号:
package net;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.SocketException;

import app.Application;

/**
 * 客户端的发送器
 * 
 * @author Thihy
 * 
 */
public class ClientSend extends Thread {
	protected PaintClient owner;
	protected Socket socket;
	protected boolean closeSocket;

	protected Object obj;
	public String netName;

	public ClientSend(PaintClient owner, Socket socket) {
		this.owner = owner;
		this.socket = socket;
		obj = null;
		closeSocket = false;
	}
	/**
	 * 将消息发送给服务器
	 * @param netMes 待发送的消息
	 */
	public void sendMessage(NetMessage netMes) {
		synchronized (this) {
			this.obj = netMes;
			notify();
		}
	}

	@Override
	public void run() {
		try {

			ObjectOutputStream oos = new ObjectOutputStream(socket
					.getOutputStream());

			synchronized (this) {
				while (true) {
					while (obj == null) {
						try {
							wait();
						} catch (InterruptedException e2) {
							if (Application.DEBUG > 0)
								e2.printStackTrace();
						}
					}
					oos.writeObject(obj);
					obj = null;
				}
			}

		} catch (SocketException e) {
			// 只是socket关闭的异常
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			System.out.println("ClientSend End");
		}
	}
}

⌨️ 快捷键说明

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