基于Tcp/IP的JAVA简单聊天程序

2008-02-23 09:40:56来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

/*Copyright 2005
*make by cat lee
*基于TCP/IP的简单聊天程序
*使用方法:先启动Server端.然后再启动Client端.
*程序有很多错误,做得很马虎,本来想把发送文件加上去的.但又要做网络版坦克小战了
*我在Java中做不出好像QQ那样的发送消息热键,本来打算用keylistener监听F8键来发消息的
*但不知道为什么不行..郁闷.
*至于界面,本来想用 IFrame做得漂亮点的,但想想程序这么烂,界面花巧有什么用...还是算了
*IDE我是用JC3.10,不支持中文.所以注释用英文,有兴趣看本烂程序的将就一下吧.^_^
*/
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.Swing.*;
import java.awt.event.*;
import javax.swing.event.*;

public class NetWork extends JFrame
{

JRadioButton rButton[] = new JRadioButton[2];
JTextField State,cHost,SMSField;
JButton Connect,DisConnect,SendMsg,SendFile;
JTextArea MessageTextArea;
ServerSocket sSocket;
Socket InSocket, cSocket;
BufferedReader bufReader;
PrintWriter pWriter;
String InStr;
int type = JOptionPane.ERROR_MESSAGE;
String title = "Error Message";
String ErrorMsg = "Error";
Container c = new Container();
NetWork_Client Client;
NetWork_Server Server;

public NetWork()
{//
super("TCP Communicate");
//Layout

c.setLayout(null);
setSize(500 , 480);
getContentPane().add(c);

MessageTextArea = new JTextArea();
MessageTextArea.setBounds(100 , 130 , 200 , 200);
MessageTextArea.setLineWrap(true);

c.add(MessageTextArea);
//c.add(new JScrollPane(MessageTextArea));
//the exit event
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent ClosingWin)
{
System.exit(0);
}
});


//intERPhase
String bStr[] = {"Server", "Client"};
ButtonGroup bGroup = new ButtonGroup();
for(int i= 0;i<2;i )
{
rButton[i] = new JRadioButton(bStr[i]);
rButton[i].setBounds(10 i*80 , 30 , 80 , 20);
bGroup.add(rButton[i]);
c.add(rButton[i]);
}
rButton[0].setSelected(true);

JLabel ConnectState = new JLabel("ConnectState :");
ConnectState.setBounds(200 , 30 , 100 , 20 );
c.add(ConnectState);
State = new JTextField();
State.setBounds(320, 30 , 150 , 20);
c.add(State);

JLabel ConnectHost = new JLabel("ConnectHost IP");
ConnectHost.setBounds(10 , 80 , 100 , 20);
c.add(ConnectHost);
cHost = new JTextField();
cHost.setBounds(100 , 80 , 200 , 20);
c.add(cHost);
Connect = new JButton("Connect");
Connect.setBounds(320 , 80 , 100 , 20);
c.add(Connect);
Connect.addActionListener(new ActionListener()
{//the event of the connect button
public void actionPerformed(ActionEvent e)
{
try
{
if(rButton[0].isSelected() == true)
{//start up the Server Thread
InStr = "";
SMSField.setText("");
Server.start();
}
else
{//start up the Client thread
InStr = "";
SMSField.setText("");
Client.start();
}
}
catch(Exception Ex)
{
return;
}
finally
{

DisConnect.setEnabled(true);
SendMsg.setEnabled(true);
Connect.setEnabled(false);

}
}
});

JLabel ReceInfo = new JLabel("Information ");
ReceInfo.setBounds(10 , 130 , 100 , 20);
c.add(ReceInfo);

DisConnect = new JButton("DisConnect");
DisConnect.setBounds(320 , 130 , 100 , 20);
c.add(DisConnect);
DisConnect.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{//the event of the disconnect button
//close the server or client
try
{
if(rButton[0].isSelected() == true)
{//close the server
InSocket.close();

}
else
{//close the client
cSocket.close();
}
}
catch(Exception Ex)
{
return;
}
finally
{
State.setText("Closed...");

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Java异常处理的陋习展播

下一篇:JSTL改进JSP来简化网页的实现过程(1)