用JAVA做个简单的私聊程序

AB为客户端,X为服务器端,A给X发送信息,能在B上显示出来!... AB为客户端,X为服务器端,A给X发送信息,能在B上显示出来! 展开
 我来答
大时代暑
2010-09-24 · TA获得超过2.5万个赞
知道大有可为答主
回答量:4111
采纳率:0%
帮助的人:4822万
展开全部
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

public class ChatClient extends Frame {

Socket s = null;
DataInputStream dis = null;
DataOutputStream dos = null;
private boolean bconnected = false;
TextField tf = new TextField();
TextArea ta = new TextArea();

public static void main(String[] args) {
new ChatClient().launchFrame();
}

public void launchFrame() {
setBounds(400,300,300,300);
add(tf,BorderLayout.SOUTH);
add(ta,BorderLayout.NORTH);
addWindowListener (new WindowAdapter() {
public void windowClosing(WindowEvent e) {
disConnect();
System.exit(0);
}
});
tf.addActionListener(new TFText());
setVisible(true);
connect();
new Thread(new ReceiveThread()).start();
}

public void connect() {
try {
s = new Socket("127.0.0.1", 8888);
dis = new DataInputStream (s.getInputStream());
dos = new DataOutputStream (s.getOutputStream());
System.out.println("connect");
bconnected = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public void disConnect() {
try {
dis.close();
dos.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}

private class TFText implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str = tf.getText().trim();
tf.setText("");
try {
dos.writeUTF (str);
dos.flush ();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

private class ReceiveThread implements Runnable {
public void run() {
try {
while (bconnected) {
String str = dis.readUTF();
ta.setText(ta.getText()+str+'\n');
}
} catch (SocketException e) {
System.out.println("退出了");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

import java.io.*;
import java.net.*;
import java.util.*;

public class ChatServer {
ServerSocket ss = null;
boolean started = false;
List<Client> clients = new ArrayList<Client>();

public static void main(String[] args) {
new ChatServer().start();
}

public void start() {
try {
ss = new ServerSocket(8888);
started = true;
} catch (BindException e) {
System.out.println(" 端口已经被占用");
System.exit(0);
}
catch (IOException e) {
e.printStackTrace();
}

try {
while (started) {
Socket s = ss.accept();
Client c = new Client (s);
System.out.println("a client is connected");
new Thread(c).start();
clients.add(c);
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

class Client implements Runnable {
private Socket s = null;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean bConnected = false;
Client (Socket s) {
this.s = s;
try {
dis = new DataInputStream (s.getInputStream());
dos = new DataOutputStream (s.getOutputStream());
bConnected = true;
} catch(IOException e) {
e.printStackTrace();
}
}

public void send (String str) {
try {
dos.writeUTF(str);
} catch(IOException e) {
clients.remove(this);
System.out.println("对方已经退出了");
}
}
public void run() {
try {
while (bConnected) {
String str = dis.readUTF();
System.out.println(str);
for (int i=0; i<clients.size(); i++) {
Client c = clients.get(i);
c.send(str);
}
}
} catch (EOFException e) {
System.out.println("client is closed!");
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (dis != null) dis.close();
if (dos != null) dos.close();
if (s != null) s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

你需要启动2个客户端
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
最低标配
2010-09-24 · TA获得超过709个赞
知道小有建树答主
回答量:496
采纳率:0%
帮助的人:199万
展开全部
import java.io.*;
import java.net.*;
public class ClientTest {
public static void main(String[] args){
Socket r1=null;
try{
r1=new Socket("127.0.0.1",8888);
}catch(Exception e){
System.out.println("error:"+e);
}
BufferedReader t1=null;
BufferedReader t2=null;
PrintWriter t3=null;
try{
t1=new BufferedReader(new InputStreamReader(System.in));
t2=new BufferedReader(new InputStreamReader(r1.getInputStream()));
t3=new PrintWriter(r1.getOutputStream());
}catch(Exception e){
e.printStackTrace();
}
String s1,s2;
System.out.println("dsag");
try{
do{
s2=t2.readLine();
System.out.println(s2);
s1=t1.readLine();
t3.println(s1);
t3.flush();
}while(!s1.equals("bye"));
r1.close();
t1.close();
t2.close();
}catch(Exception e){
System.out.println("错误!");
}
}
}
另一个服务端:
import java.net.*;
import java.io.*;
public class ServerSocketTest {//原来叫ServerSocket就发生了冲突所以要小心!
public static void main(String[] args){
ServerSocket r1=null;
Socket r2=null;
try{
r1=new ServerSocket(8888);
//while(true){
r2=r1.accept();
System.out.println("连接成功!你先输入");
// }
}catch(Exception e){
System.out.println("没有连接成功!");
System.exit(1);
}

try{
PrintWriter t1=new PrintWriter(r2.getOutputStream());
BufferedReader t2=new BufferedReader(new InputStreamReader(System.in));
BufferedReader t3=new BufferedReader(new InputStreamReader(r2.getInputStream()));
String s1,s2;

do{
s1=t2.readLine();
t1.println(s1);
t1.flush();
s2=t3.readLine();
System.out.println(s2);
}while(!s1.equals("bye"));
r1.close();
r2.close();
t1.close();
t2.close();
}catch(Exception e){
System.out.println("错误!");
}

}
}
很简单的程序!自己和自己说话!很好玩的 当说“bye”时程序就会结束!
要先运行服务端 先运行客户端!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
LinShara
2010-09-29
知道答主
回答量:81
采纳率:0%
帮助的人:0
展开全部
也可以考虑非阻塞
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
sharaloving
2010-09-25
知道答主
回答量:46
采纳率:0%
帮助的人:0
展开全部
别忘了先启动服务器端,再启动客户端
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式