分享
 
 
 

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

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

/*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);

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有