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

📄 mysocket.java

📁 黑白棋123 黑白棋456 大学毕业设计 做很好
💻 JAVA
字号:

//*******************************************
//
//  MySocket.java
//
//	          包含MySocket类, 负责网络连接
//
//*******************************************

import java.io.*;
import java.io.InterruptedIOException;
import java.net.*;
import OthelloPanel;

public class MySocket extends Thread 
{
	final static int MAXCLIENT = 20;
	
	Socket m_socket;
	BufferedReader m_input;
	BufferedWriter m_output;
	OthelloPanel main;
	
	int m_id;
	String m_client[];
	
	boolean quit;
		
	String txt;
	int type;
	String obj;
	
	public MySocket(OthelloPanel _m)
	{
		m_client = new String [20];		
		
		main = _m;
		quit = false;
	}
	
	public boolean initNet()
	{
		try
		{
			InetAddress host;
			host = InetAddress.getByName( main.applet.getCodeBase().getHost());
			
			m_socket = new Socket(host,3000);
			m_input = new BufferedReader( new InputStreamReader ( m_socket.getInputStream()) );
			m_output = new BufferedWriter ( new OutputStreamWriter( m_socket.getOutputStream()) );
		//	main.connecting=true;
			
		}
		catch(Exception e)
		{
			MessageBox.createMessageBox("无法连接到主机 !" , "连接失败");
			main.m_ChatFrm.OnDisconnect();
			return  false;
		}
		
		// 向主机发送自己的名字
		String msg = WriteMsg(3,"",main.m_ChatFrm.m_name);
		this.Send(msg);
		System.out.println("Send name to server");
		return true;
	}
	
	
	//数据预处理
	//消息结构
	// MSG /type (byte), /obj (byte)  ,/text (String)
	public void TransMsg(String str)
	{
		type = 0;
		obj = "";
		txt = "";
		int i;
		String head = new String ();
		int len = str.length ();
		
		if (len < 5 )
			return;
		
		head="";
		head+=str.charAt(0);
		head+=str.charAt(1);
		head+=str.charAt(2);
	
		if (!head.equals("MSG")) 
		{
			System.out.print("     Receive Error Message : ");
			System.out.println(str);
			return;
		}
		
		// type
		i=3;
		String _type = new String();
		while(str.charAt(i) != '\\')
		{
			_type+=str.charAt(i);
			i++;
		}
		i++;
		type = (Integer.decode(_type)).intValue();
		
		
		// obj 
		while(str.charAt(i) != '\\')
		{
			obj+=str.charAt(i);
			i++;
		}
		i++;
		
		// txt
		while(i<len)
		{
			txt+=str.charAt(i);
			i++;
		}
		
	}
	
	
	//写输出数据缓冲
	//消息格式
	// \MSG \type (int) \obj (int) \0   \txt(String)
	public String WriteMsg(int type,String obj,String txt)
	{
		String buf = new String("MSG");
		Integer _type = new Integer(type);
		buf +=_type.toString();
		buf += '\\';
		buf += obj;
		buf += '\\';
		
		buf += txt;
		
		char[] d = new char[buf.length()];
		buf.getChars(0,buf.length()-1,d,0);
		
		System.out.println(buf);
		return buf;
	}
	
	//向服务器发送自己的名字

	
	//发送数据
	public void Send(int type,String obj,String str)
	{
		String msg = this.WriteMsg(type,obj,str);
		Send(msg);
	}
	
	
	public void Send(String str)
	{

		str+="\n";
		try
		{
			m_output.write ( str);
			m_output.flush();
		}
		catch(IOException e)
		{
			System.out.println("Error at sending : "+e);
			System.out.println (" Disconnect with server");
			main.m_ChatFrm.OnDisconnect();
			return;
		}
		
		System.out.println("Sended data");
	}
	
	//丛输入流读一个字节
	public String ReceiveChar()
	{	
		char chars[];
		int num;
		try
		{
			chars= new char[1];
			num = m_input.read(chars,0,1);
			
			if (num>0)
			{
				System.out.println( (new String(chars) )  );
				return (new String( chars ));
			}
			else
			{
				return null;
			}
		}
		catch(IOException e)
		{
			main.m_ChatFrm.OnDisconnect();
		}
		return null;
				
	}
	
	//从输入流读一行
	public String Receive()
	{
		String buf = null;
		try
		{
			buf = m_input.readLine();
		}
		catch(IOException e)
		{
			quit = true;

		}
		return buf;
	}
	
	
	// run
	public void run()
	{
		String str = new String();
		while(!quit)
		{
			System.out.println("Begin to receive");
			str = Receive();
			if(str != null )
			{
				TransMsg(str);
				main.DealInput(type,txt);
			}
			else
			try
			{
				sleep(50);
			}
			catch(InterruptedException e)
			{
				
			}
		}
		
		_stop();
	}
	
	public void _stop()
	{
		String msg = WriteMsg(5,"","");
		Send(msg);
		
		try
		{
			m_input.close();
			m_output.close();
		
			m_socket.close();
		}
		catch(IOException e)
		{
			System.out.println("Close Error : "+e);
		}

		main.m_socket = null;
		main.m_ChatFrm.m_bDisconnect.setEnabled(false);
		main.m_ChatFrm.m_bReconnect.setEnabled(true);
		main.m_ChatFrm.m_bPlayOnNet.setEnabled(false);
		
		main.m_ChatFrm.m_obj.removeAll();
		main.m_ChatFrm.m_text.append("       到主机的连接已中断\n");
		main.m_ChatFrm.setTitle("Net  ---  未连接");

		super.stop();
	}
}

⌨️ 快捷键说明

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