跳转qq群java代码 跳转聊天代码( 三 )


button_send.addActionListener(this);
addWindowListener(this);
this.setTitle("客户端");
setBounds(200,200,400,350);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == button_connect)
{
try
{
String ip = null,port = null;
ip = text_ip.getText();
port = text_port.getText();
mysocket = new Socket(ip, Integer.parseInt(port));
in = new DataInputStream(mysocket.getInputStream());
out = new DataOutputStream(mysocket.getOutputStream());
}
catch (UnknownHostException ex)
{
Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex)
{
Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex);
}
mythread.start();
}
if(e.getSource() == button_send)
{
if(mysocket.isConnected() == true)
{
String temp = null;
temp = text_send.getText();
try
{
out.writeUTF(temp);
text_send.setText(null);
}
catch (IOException ex)
{
Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public void run()
{
while(true)
{
String temp = null;
try
{
temp = in.readUTF();
}
catch (IOException ex)
{
text_area_show.append("服务器退出\n");
return;
}
temp += "\n";
text_area_show.append(temp);
}
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_ENTER)
{
String temp = null;
temp = text_send.getText();
try
{
out.writeUTF(temp);
}
catch (IOException ex)
{
Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowOpened(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
}
public class mywindow
{
public static void main(String argv[])
{
myframe f = new myframe();
}
}
-----------客户端-----------
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
class SocketList
{
int length = 0;
Socket socketlist[] = new Socket[10];
SocketList()
{
}
void add(Socket s)
{
if(length == 10)
{
return;
}
socketlist[length] = s;
length++;
}
void Delete(Socket s)
{
int i = 0;
for(i = 0;ilength;i++)
{
if(socketlist[i] == s)
{
break;
}
}
for(int j = i;jlength;j++)
{
socketlist[j] = socketlist[j + 1];
}
length--;
/*
for(int k = 0;klength;k++)
{
if(socketlist[k].isBound() == true)
{
System.out.print("error");
}
}*/
}
}
class FrameThread extends Thread
{
myframe frame;
FrameThread(myframe f)
{
frame = f;
start();
}
public void run()
{
while(true)
{
InetAddress addr = null;
Socket s = null;
try
{
s = frame.server.accept();
addr = s.getInetAddress();
frame.socketlist.add(s);
for(int i = 0;iframe.socketlist.length;i++)
{

推荐阅读