用eclipse软件java聊天程序代码的客户端和服务器怎么设置,java一对一聊天程序,客户端发的消息无法在服务端显示 小弟我用的是eclipse...

当前位置:我的异常网» Eclipse » java一对一聊天程序,客户端发的消息无法在服务端显

java一对一聊天程序,客户端发的消息无法在服务端显示 小弟我用的是eclipse

www.myexceptions.net  网友分享于:2013-01-15  浏览:28次

java一对一聊天程序,客户端发的消息无法在服务端显示 我用的是eclipse

服务端:

import java.io.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

import java.net.*;

public class L_serve implements ActionListener

{

JTextArea said;

JTextField chat;

JButton send;

public L_serve()

{

JFrame f=new JFrame("JAVA QQ服务端");

f.setLayout(null);

f.setSize(450,400);

said=new JTextArea();

said.setBounds(0,0,450,200);

chat=new JTextField(20);

chat.setBounds(0,210,450,100);

chat.addActionListener(this);

send=new JButton("发送");

send.setBounds(100,320,70,20);

send.addActionListener(this);

f.add(chat);

f.add(said);

f.add(send);

f.setVisible(true);

}

public void listenClient() throws IOException

{

try{

ServerSocket server=null;

try{

server=new ServerSocket(7641);//创建一个ServerSocket在端口4700监听客户请求

}catch(Exception e) {

System.out.println("can not listen t"+e); //在屏幕上显示出错信息

said.append("duankou"+e);

}

Socket socket=null;

try{

socket=server.accept();//使用accept()阻塞等待客户请求,有客户

//请求到来则产生一个Socket对象,并继续执行

}catch(Exception e) {

System.out.println("Error."+e);

said.append("ERROR."+e);

}

String line;

BufferedReader in=new BufferedReader(new InputStreamReader(

socket.getInputStream()));

//由Socket对象得到输入流,并构造相应的BufferedReader对象

PrintWriter out=new PrintWriter(socket.getOutputStream());

//由Socket对象得到输出流,并构造PrintWriter对象

BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));

//由系统标准输入设备构造BufferedReader对象

System.out.println("Client:"+in.readLine());//在标准输出上打印从客户端读入的字符串

said.append("Client:"+in.readLine());

line=sin.readLine(); //从标准输入读入一字符串

while(!line.equals("bye")){ //如果该字符串为 "bye",则停止循环

out.println(line); //向客户端输出该字符串

out.flush(); //刷新输出流,使Client马上收到该字符串

System.out.println("Server:"+line); //在屏幕上显示读入的字符串

said.append("Server:"+line);

System.out.println("Client:"+in.readLine());//从Client读入一字符串,并打印到标准输出上

said.append("Client:"+in.readLine());

line=sin.readLine(); //从系统标准输入读入一字符串

} //继续循环

out.close(); //关闭Socket输出流

in.close(); //关闭Socket输入流

socket.close(); //关闭Socket

server.close(); //关闭ServerSocket

}catch(Exception e){

System.out.println("Error:"+e);

}

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==send){

String str=chat.getText();

said.append("server:"+str+'\n');}

chat.setText(null);

}

public static void main(String args[]) throws IOException

{

L_serve s=new L_serve();

s.listenClient();

}

}

客户端:

import java.io.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.net.*;

public class L_client implements ActionListener

{

文章评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值