| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 664 人关注过本帖
标题:聊天室问题
取消只看楼主 加入收藏
野蛮女人
Rank: 5Rank: 5
等 级:贵宾
威 望:19
帖 子:827
专家分:0
注 册:2007-4-7
结帖率:100%
收藏
 问题点数:0 回复次数:5 
聊天室问题

兄弟们帮我看看到底怎么回事 我总是弄不好啊

package net;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class MyClient extends JFrame implements ActionListener {
private JTextField f1;

private JTextArea a1;

private JScrollPane s1;

private PrintWriter out;

private BufferedReader in;

private Socket socket;

public MyClient() {
createView();
try {
socket = new Socket("localhost", 8888);
out = new PrintWriter(socket.getOutputStream());
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
new Thread(new Runnable() {
public void run() {
receive();
}
}).start();
} catch (IOException e) {
e.printStackTrace();
}
}

public boolean send(String str) {
out.print(str);
out.flush();
return true;
}

public void receive() {
System.out.println("===== Clinte receive Begin =====");
String Message = null;
while (true) {
try {
Message = in.readLine();
a1.append(Message+"\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}

private void createView() {
f1 = new JTextField(30);
a1 = new JTextArea(35, 35);
s1 = new JScrollPane(a1);
getContentPane().add(f1, "North");
getContentPane().add(s1, "Center");
setSize(300, 300);
setVisible(true);
f1.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent arg0) {
String s = f1.getText();
a1.append("clinet:" + s + "\n");
f1.setText("");
send(s + "\n");
}

public static void main(String[] args) {
new MyClient();
}
}
=====================
package net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import java.net.ServerSocket;
import java.net.Socket;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Vector;

public class MyServer {

private Socket socket;
private BufferedReader read;
private PrintWriter writer;
private LinkedList<Socket> list = new LinkedList<Socket>();

public MyServer(){
ServerSocket ssocket = null;
try {
ssocket = new ServerSocket(8888);
} catch (IOException e2) {
e2.printStackTrace();
}
while(true){
try {
socket = ssocket.accept();
list.add(socket);
new Thread(new Runnable(){
public void run() {
try {
receive();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

public void receive() throws IOException{
while(true){
read = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String Message = read.readLine();
System.out.println("====Server Recevie======"+Message);
send(Message);
}
}

private void send(String Message){
for(int i=0;i<list.size();i++){
try {
Socket sendSocket = (Socket) list.get(i);
writer = new PrintWriter(sendSocket.getOutputStream());
writer.print(new String("ServerSpeak:"+Message+"\n"));
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) {
MyServer server = new MyServer();
}
}

搜索更多相关主题的帖子: 聊天室 
2007-06-16 20:58
野蛮女人
Rank: 5Rank: 5
等 级:贵宾
威 望:19
帖 子:827
专家分:0
注 册:2007-4-7
收藏
得分:0 
而且每次我关闭客户端都会有这个异常
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder$CharsetSD.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at net.MyServer.receive(MyServer.java:51)
at net.MyServer$1.run(MyServer.java:36)
at java.lang.Thread.run(Unknown Source)

[shadow=255,purple,5]好人不长命,祸害一万年![/shadow]
2007-06-16 21:20
野蛮女人
Rank: 5Rank: 5
等 级:贵宾
威 望:19
帖 子:827
专家分:0
注 册:2007-4-7
收藏
得分:0 

没人理我啊


[shadow=255,purple,5]好人不长命,祸害一万年![/shadow]
2007-06-16 22:03
野蛮女人
Rank: 5Rank: 5
等 级:贵宾
威 望:19
帖 子:827
专家分:0
注 册:2007-4-7
收藏
得分:0 
哦 这个我知道了 这个程序还有其他的问题 你帮我看看

[shadow=255,purple,5]好人不长命,祸害一万年![/shadow]
2007-06-16 22:35
野蛮女人
Rank: 5Rank: 5
等 级:贵宾
威 望:19
帖 子:827
专家分:0
注 册:2007-4-7
收藏
得分:0 

没人理我啊 千里来指点一下啊 我用了你的办法还是有问题啊


[shadow=255,purple,5]好人不长命,祸害一万年![/shadow]
2007-06-17 10:28
野蛮女人
Rank: 5Rank: 5
等 级:贵宾
威 望:19
帖 子:827
专家分:0
注 册:2007-4-7
收藏
得分:0 

没人理我 啊


[shadow=255,purple,5]好人不长命,祸害一万年![/shadow]
2007-06-17 18:51
快速回复:聊天室问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031750 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved