/*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...");
DisConnect.setEnabled(false);
SendMsg.setEnabled(false);
Connect.setEnabled(true);
}
}
});
SMSField = new JTextField();
SMSField.setBounds(100 , 350 , 200 , 20);
SMSField.setToolTipText("Enter your message here");
c.add(SMSField);
// send message
SendMsg = new JButton("Send");
SendMsg.setBounds(320,350 , 100 , 20);
SendMsg.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
int key = e.getKeyCode();
if(key == 119)
{
try
{
pWriter.write(SMSField.getText());
pWriter.write("\n");
pWriter.flush();
SMSField.setText("");
}
catch(NullPointerException NpEx)
{
return;
}
}
}
public void keyReleased(KeyEvent e)
{
}
});
SendMsg.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
pWriter.write(SMSField.getText());
pWriter.write("\n");
pWriter.flush();
if(rButton[0].isSelected() == true)
{
MessageTextArea.setText("(Server) :"+SMSField.getText());
}
else
{
MessageTextArea.setText("(Client) :"+SMSField.getText());
}
SMSField.setText("");
}
catch(NullPointerException NpEx)
{
return;
}
}
});
c.add(SendMsg);
//send file
/*
SendFile = new JButton("Send File");
SendFile.setBounds(320 , 300 , 100,20);
SendFile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFileChooser FileCh = new JFileChooser();
FileCh.setDialogTitle("Open File");
FileCh.setApproveButtonText("Open");
FileCh.showOpenDialog(c);
File FileName = FileCh.getSelectedFile();
FileReader FRead = null;
FileInputStream Input = null;
try
{
Input = new FileInputStream(FileName);
}
}
});
c.add(SendFile);
*/
JLabel about = new JLabel("Make by cat lee");
about.setBounds(50 , 400 , 100 , 20);
c.add(about);
//set up the server and client thread
Client = new NetWork_Client();
Server = new NetWork_Server();
}
//The Server Thread
class NetWork_Server extends Thread
{
public NetWork_Server()
{
}
public void run()
{
try
{
Connect.setEnabled(true);
State.setText("Waiting for Connect");
cHost.setText(InetAddress.getLocalHost().getHostAddress());
sSocket = new ServerSocket(1984);
InSocket = sSocket.accept();
bufReader = new BufferedReader(new InputStreamReader(InSocket.getInputStream()));
pWriter = new PrintWriter(InSocket.getOutputStream() , true);
while(true)
{
if(sSocket.isBound() == true)
{//makesure is bound
State.setText("Connecting");
DisConnect.setEnabled(true);
Connect.setEnabled(false);
SendMsg.setEnabled(true);
break;
}
}
while(true)
{
InStr = bufReader.readLine();
if(InStr.length() >0 )
{
MessageTextArea.setText("(Client) :");
MessageTextArea.append(InStr);
MessageTextArea.append("\n");
}
}
}
catch(Exception Ex)
{
if(sSocket.isBound() == false)
{
ErrorMsg = "Disconnected";
State.setText("Closed...");
DisConnect.setEnabled(false);
Connect.setEnabled(true);
//InSocket.close();
JOptionPane.showMessageDialog(c , ErrorMsg,title,type);
}
return;
}
}
}
//The Client Thread
class NetWork_Client extends Thread
{
public NetWork_Client()
{
}
public void run()
{
try
{
Connect.setEnabled(false);
State.setText("Waiting for Connect");
cSocket = new Socket();
cSocket.connect(new InetSocketAddress(cHost.getText() , 1984) ,1985 );
bufReader = new BufferedReader(new InputStreamReader(cSocket.getInputStream()));
pWriter = new PrintWriter(cSocket.getOutputStream() , true);
while(true)
{//makesure is connected
if(cSocket.isConnected() == true)
{
State.setText("Connecting");
DisConnect.setEnabled(true);
Connect.setEnabled(false);
SendMsg.setEnabled(true);
break;
}
}
InStr = "";
while(true)
{
InStr = bufReader.readLine();
if(InStr.length() > 0)
{
MessageTextArea.setText("(Server) :");
MessageTextArea.append(InStr);
MessageTextArea.append("\n");
}
}
}
catch(Exception Ex)
{
ErrorMsg = "Disconnected";
JOptionPane.showMessageDialog(c , ErrorMsg,title,type);
return;
}
}
}
public static void main(String args[])
{
NetWork nw = new NetWork();
nw.setVisible(true);
nw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
nw.setResizable(false);
}
}