📄 30fe75603ed700191f48b4d0fe87e2ab
字号:
/*
* Created on 2005-5-31
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package client;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.net.*;
import java.io.*;
class TCPConnection extends Thread
{
//服务器主机地址,由主程序给出
private String host = null;
//服务器端口号,由主程序给出
private int portNumber = 0;
//判断是否成功建立连接的变量
boolean connectSuccess = true;
//指向主程序的引用
private PublicChatGUI tcpGUI = null;
//客户端Socket
private Socket clientSocket = null;
//输入输出流
InputStream inputStream = null;
// OutputStream outputStream = null;
// DataInputStream dataInputStream = null;
BufferedReader dataInputReader = null;
PrintWriter dataOutputWriter = null;
// PrintStream printStream = null;
//输入输出缓冲区
String messageGot;
byte[] byteIn = new byte[1024];
//这种设计有问题,因为如果连接有问题,要重连时又要生成TCPConnection对象,这样的设计会出现问题:
//*************singleton设计模式**********************
// private static TCPConnection conn = null;
// static boolean noException = true;
TCPConnection(PublicChatGUI gui, String host, int portNumber)
{
tcpGUI = gui;
this.host = host;
this.portNumber = portNumber;
//建立连接,如果连连接都建立不起来,就直接返回,不再建立输入输出流
try
{
clientSocket = new Socket(host, portNumber);
} catch(UnknownHostException e)
{
connectSuccess = false;
tcpGUI.appendMessageAreaText(PublicChatGUI.EXCEPTION + "未知主机,请点击\"选项\\服务器地址\"进行配置,然后重新连接\n");
return ;
} catch(IOException e)
{
connectSuccess = false;
tcpGUI.appendMessageAreaText(PublicChatGUI.EXCEPTION + "无法建立数据通路,端口错误或主机服务不存在请点击\"选项\\服务器TCP端口\"进行配置,然后重新连接\n");
return ;
}
//得到输入输出流,如若失败,就关闭连接
try
{
dataInputReader =new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
dataOutputWriter = new PrintWriter(clientSocket.getOutputStream());
} catch(IOException e)
{
connectSuccess = false;
tcpGUI.appendMessageAreaText(PublicChatGUI.EXCEPTION + "无法从Socket得到输入输出流,将关闭连接..\n");
try
{
clientSocket.close();
} catch(IOException eclose)
{
tcpGUI.appendMessageAreaText("关闭连接失败\n");
}
}
}
/*static TCPConnection getTCPConnection(PublicChatGUI gui, String host, int portNumber)
{
if (conn == null)
{
conn = new TCPConnection(gui, host, portNumber);
if (noException == false)
{
try
{
conn.closeSocket();
} catch(Throwable e)
{
JOptionPane.showMessageDialog(null, "销毁对象TCPConnection时出错", "Error", JOptionPane.OK_OPTION);
}
conn = null;
}
}
return conn;
}*/
//***************************************************
//关闭连接,释放资源
public void closeSocket()
{
try
{
clientSocket.close();
} catch(IOException e)
{
tcpGUI.appendMessageAreaText("关闭连接时出错\n");
}
}
//向服务器发送登陆信息,如果用户名无重复,返回true
boolean sendLoginInfo(String screenName)
{
boolean noSameName = true;
//System.out.println( "shit " + screenName );
tcpGUI.tcpConnection.dataOutputWriter.println("HELO " + screenName);
tcpGUI.tcpConnection.dataOutputWriter.flush();
try
{
// noSameName = dataInputStream.readBoolean();
String trueOrFalse = dataInputReader.readLine();
System.out.println("dataInputReader.readLine() " + trueOrFalse);
noSameName = trueOrFalse.equalsIgnoreCase("true");
// System.out.println("noSameName" + noSameName);
} catch(IOException e)
{
tcpGUI.appendMessageAreaText(PublicChatGUI.EXCEPTION + "从服务器得到连接确认信息时,出现了异常,即将关闭连接\n");
tcpGUI.disConnect();
}
return noSameName;
}
/* //从服务器接收欢迎消息&端口号
int getServerUDPPort()
{
int serverUDPPort = tcpGUI.udpPort;
String welcomMessage;
try
{
welcomMessage = dataInputStream.readLine();
} catch(IOException e)
{
tcpGUI.setMessageAreaText(PublicChatGUI.EXCEPTION + "确认欢迎&端口信息时出现输入输出异常,将使用默认UDP端口号");
}
return serverUDPPort;
}*/
//从服务器接收欢迎&udp端口消息
int getWelcomAndUDPPort()
{
int udpPort = 0;
try
{
messageGot = tcpGUI.tcpConnection.dataInputReader.readLine();
tcpGUI.appendMessageAreaText(messageGot.substring(messageGot.indexOf("WELCOME ") + 8));
} catch(IOException e)
{
tcpGUI.appendMessageAreaText(PublicChatGUI.EXCEPTION + "确认欢迎&端口信息时出现输入输出异常,将使用默认UDP端口号\n");
}
try
{
udpPort = Integer.parseInt(messageGot.substring(messageGot.indexOf("WELCOME ") + 8));
} catch(NumberFormatException nfe)
{
tcpGUI.appendMessageAreaText(PublicChatGUI.EXCEPTION + "提取UDP端口号出现异常\n");
}
return udpPort;
}
//从服务器接收用户列表:
//一切就绪,启动线程,接受服务器发过来的消息:
/*//向服务器发送消息,UDP实现
void sendTextMessage(String msg)
{
printStream.println("HELO <" + msg + ">");
}*/
//等待服务器端的消息
ObjectInputStream objectInput = null;
public void run()
{
while(true)
{
try
{
String messageStr = dataInputReader.readLine();
//debug:
System.out.println("now" + messageStr);
if (messageStr.indexOf("SERVERQUIT") != -1)
{
tcpGUI.appendMessageAreaText("服务器从连接上主动断开!!!\n" + "请尝试另外的主机地址或TCP端口");
tcpGUI.disConnect();
}
if (messageStr.indexOf("ADD ") != -1)
{
String name = messageStr.substring(messageStr.indexOf("ADD ") + 4);
tcpGUI.appendMessageAreaText(name + "登录了!");
tcpGUI.lItems.add(0, name);
}
if (messageStr.indexOf("DEL ") != -1)
{
String name = messageStr.substring(messageStr.indexOf("DEL ") + 4);
tcpGUI.appendMessageAreaText(name + "下线了!");
tcpGUI.lItems.removeElement(name);
}
if (messageStr.indexOf("PUBLIC ") != -1)
{
String name = messageStr.substring(messageStr.indexOf("PUBLIC ") + 7, messageStr.indexOf(":"));
tcpGUI.appendMessageAreaText(name + "说: " + messageStr.substring(messageStr.indexOf(":") + 2));
}
if (messageStr.indexOf("PRIVATE ") != -1)
{
tcpGUI.appendMessageAreaText("herer");
String sender = messageStr.substring(messageStr.indexOf("FROM ") + 5);
String qiaoQiaoHua = messageStr.substring(messageStr.indexOf("MSG ") + 4);
tcpGUI.appendMessageAreaText(sender + "给你说了一句悄悄话: " + qiaoQiaoHua);
}
} catch(IOException e)
{
tcpGUI.appendMessageAreaText(PublicChatGUI.EXCEPTION + "得到聊天信息时出错\n");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -