if(frame.socketlist.socketlist[i] != s )
{
DataInputStream in = new DataInputStream(frame.socketlist.socketlist[i].getInputStream());
DataOutputStream out= new DataOutputStream(frame.socketlist.socketlist[i].getOutputStream());
out.writeUTF(addr + "连接到服务器");
}
}
}
catch (IOException ex)
{
Logger.getLogger(FrameThread.class.getName()).log(Level.SEVERE, null, ex);
}
frame.text_area_show.append(addr + "连接到服务器\n");
try
{
frame.list.add(InetAddress.getLocalHost().getHostAddress() + "-" + s.getPort());
}
catch (UnknownHostException ex)
{
Logger.getLogger(FrameThread.class.getName()).log(Level.SEVERE, null, ex);
}
try
{
new ClientThread(s, frame);
}
catch (IOException ex)
{
Logger.getLogger(FrameThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
class ClientThread extends Thread
{
DataInputStream in = null;
DataOutputStream out = null;
Socket mysocket = new Socket();
myframe frame = null;
InetAddress addr = null;
ClientThread(Socket s,myframe f) throws IOException
{
mysocket = s;
addr = mysocket.getLocalAddress();
frame = f;
in = new DataInputStream(mysocket.getInputStream());
out = new DataOutputStream(mysocket.getOutputStream());
out.writeUTF("成功连接服务器");
start();
}
public void run()
{
while(true)
{
String temp = null;
try
{
temp = in.readUTF();
for(int i = 0;iframe.socketlist.length;i++)
{
DataInputStream in_temp = new DataInputStream(frame.socketlist.socketlist[i].getInputStream());
DataOutputStream out_temp = new DataOutputStream(frame.socketlist.socketlist[i].getOutputStream());
out_temp.writeUTF(addr + "说: " + temp);
}
frame.text_area_show.append(addr + "说: " + temp + "\n");
}
catch (IOException ex)
{
frame.socketlist.Delete(mysocket);
frame.text_area_show.append(addr + "退出服务器\n");
for(int i = 0;iframe.socketlist.length;i++)
{
try
{
DataInputStream in_temp = new DataInputStream(frame.socketlist.socketlist[i].getInputStream());
}
catch (IOException ex1)
{
Logger.getLogger(ClientThread.class.getName()).log(Level.SEVERE, null, ex1);
}
DataOutputStream out_temp = null;
try
{
out_temp = new DataOutputStream(frame.socketlist.socketlist[i].getOutputStream());
}
catch (IOException ex1)
{
Logger.getLogger(ClientThread.class.getName()).log(Level.SEVERE, null, ex1);
}
try
{
out_temp.writeUTF(addr + "退出服务器");
}
catch (IOException ex1)
{
Logger.getLogger(ClientThread.class.getName()).log(Level.SEVERE, null, ex1);
}
}
return;
}
}
}
}
class myframe extends JFrame implements ActionListener,WindowListener,KeyListener
{
List list = new List(16);
TextArea text_area_show = new TextArea(15,40);
TextField text_send = new TextField(50);
Button button_send = new Button("发送");
ServerSocket server = null;
FrameThread thread;
SocketList socketlist = new SocketList();
myframe() throws IOException
{
Panel panel1 = new Panel();
panel1.setLayout(new FlowLayout());
panel1.add(text_area_show);
panel1.add(list);
list.addActionListener(this);
Panel panel2 = new Panel();
panel2.setLayout(new FlowLayout());
panel2.add(text_send);
panel2.add(button_send);
setLayout(new FlowLayout());
add(panel1);
add(panel2);
text_send.addKeyListener(this);
button_send.addActionListener(this);
addWindowListener(this);
server = new ServerSocket(8888);
推荐阅读
- 手机福利直播软件,手机福利直播软件有哪些
- c语言中怎么弄输出的宽度,c语言数据输出长度超过指定宽度
- 会务公司小程序怎么做,会务公司小程序怎么做推广
- mysql是怎么避免脏读 mysql怎么避免回表
- 我的迷你世界下载,我的迷你世界下载安装免费
- oracle取小的函数,oracle获取小数点后的数值
- 圣岛服务器,圣岛入口在哪里
- go语言函数与接口 go语言接口做参数
- mysql修改数据的语句,mysql修改语句汇总