📄 mysocket.java
字号:
/*******************************************
MySocket.java
包含MySocket类, 负责网络链接
*******************************************
*/
import java.io.*;
import java.io.InterruptedIOException;
import java.net.*;
//import Othello;
public class MySocket extends Thread
{
final static int MAXCLIENT = 20;
Socket m_socket;
BufferedReader m_input;
BufferedWriter m_output;
Othello main;
int m_id;
String m_client[];
boolean quit;
String txt;
int type;
String obj;
public MySocket(Othello _m)
{
m_client = new String [20];
main = _m;
quit = false;
}
public boolean initNet()
{
try
{
InetAddress host;
host = InetAddress.getByName("leehom");
m_socket = new Socket(host,3000);
m_input = new BufferedReader(
new InputStreamReader ( m_socket.getInputStream()) );
m_output = new BufferedWriter (
new OutputStreamWriter( m_socket.getOutputStream()) );
}
catch(Exception e)
{
MessageBox.createMessageBox("无法链接到主机 !" , "链接失败");
main.chat.OnDisconnect();
return false;
}
// 向主机发送自己的名字
String msg = WriteMsg(3,"",main.chat.m_name);
this.Send(msg);
System.out.println("向主机发送名字");
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 (" 收到错误信息 : ");
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("发送错误: "+e);
System.out.println (" 与服务器断开");
main.chat.OnDisconnect();
return;
}
System.out.println("数据传送完成");
}
//丛输入流读一个字节
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.chat.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("开始接收");
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("关闭流出错: "+e);
}
main.m_socket = null;
main.chat.m_bDisconnect.setEnabled(false);
main.chat.m_bReconnect.setEnabled(true);
main.chat.m_bPlayOnNet.setEnabled(false);
main.chat.m_obj.removeAll();
main.chat.m_text.append(" 到主机的链接已中断\n");
main.chat.setTitle("网络 --- 未链接");
super.stop();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -