java聊天程序的问题..请大侠们帮帮小弟...

KeroroCatboy 2007-08-14 05:21:55
一个Client客户端,一个Server服务器端...

在数据输入及输出的时候出了问题...自己找了好久都找不到...

输出流用的是bufferedWriter... 输入流用的是BufferedReader...

最后运行的时候数据无法相互发送...

但是用ObjectInputStream和ObjectOutputStream()分别替换I/0又可以了..

下面是两个类的源代码...

public TestClient(String host){
chatServer = host;
Container container = getContentPane();
inputInfo = new JTextField();
inputInfo.setEnabled(false);
inputInfo.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent e){
sendData(e.getActionCommand());
inputInfo.setText("");
}

});

container.add(inputInfo,java.awt.BorderLayout.NORTH);
showingInfo = new JTextArea();
container.add(new JScrollPane(showingInfo),BorderLayout.CENTER);
setSize(400,300);
setTitle("Client");
setVisible(true);
}

public void run(){
connectToServer();
getStream();
processConnect();
close();

}
public static void main(String[] args) {
TestClient client;
if (args.length == 0){

client = new TestClient("127.0.0.1");
}
else{
client = new TestClient(args[0]);
}
client.setDefaultCloseOperation(client.EXIT_ON_CLOSE);
client.run();
}

public void connectToServer(){
showingInfo.setText("connection...\n");
try {
client = new Socket(InetAddress.getByName(chatServer),5000);
showingInfo.append(
"Connecting to "+client.getInetAddress().getHostName());
} catch (UnknownHostException e) {

System.out.println("不知名主机.");
} catch (IOException e) {
System.out.println("输入输出流异常");
e.printStackTrace();
}

}
public void getStream(){

try {
os = new BufferedWriter( new OutputStreamWriter(client.getOutputStream()));
os.flush();
is = new BufferedReader (new InputStreamReader(client.getInputStream()));
showingInfo.append("\ngot I/O Streams");

} catch (IOException e) {
System.out.println("输入输出流异常");
e.printStackTrace();
}
}
public void processConnect(){
inputInfo.setEnabled(true);
String message;
try {

do{
message = (String) is.readLine();

showingInfo.append("\n"+message);
showingInfo.setCaretPosition(showingInfo.getText().length());
}while(! message.equals("SERVER>>>terminate"));


} catch (EOFException e){
System.out.println("EOFError");
} catch (IOException e) {
System.out.println("输入输出流异常");
e.printStackTrace();
}
}
public void sendData(String message){
try {
os.write("CLIENT>>>"+message);
os.flush();
showingInfo.append("\nCLIENT>>>"+message);

} catch (IOException e) {
System.out.println("输入输出流异常");
e.printStackTrace();
}

}
public void close(){
showingInfo.append("\nconnection terminate");
inputInfo.setEnabled(false);
try {
is.close();
os.close();
client.close();
} catch (IOException e) {
System.out.println("输入输出流异常");
e.printStackTrace();
}


}
}


public class TestServer extends JFrame{

ServerSocket serverSocket;
Socket server;
BufferedReader is;
BufferedWriter os;
JTextArea showingInfo;
JTextField inputInfo;
int count = 1;


public TestServer(){

Container container = getContentPane();

inputInfo = new JTextField();
inputInfo.setEnabled(false);
inputInfo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
sendData(e.getActionCommand());
inputInfo.setText("");
}
});
container.add(inputInfo);
showingInfo = new JTextArea();
container.add(new JScrollPane(showingInfo),BorderLayout.CENTER);
container.add(inputInfo,BorderLayout.NORTH);
setSize(400,300);
setTitle("Server");
setVisible(true);
}
public static void main(String[] args) {
TestServer server = new TestServer();
server.setDefaultCloseOperation(server.EXIT_ON_CLOSE);
server.run();

}
public void sendData(String message) {
try {
os.write("SERVER>>>"+message);
os.flush();
} catch (IOException e) {
System.out.println("输入输出流异常");
e.printStackTrace();
}
showingInfo.append("\nSERVER>>>"+message);

}
public void run(){
try {
serverSocket = new ServerSocket(5000,10);
while(true){
connectTOClient();
getStream();
processConnection();
close();
count++;
}

} catch (IOException e) {
System.out.println("输入输出流异常");
e.printStackTrace();
}

}
public void connectTOClient() {

showingInfo.setText("waiting for connection");
try {
server = serverSocket.accept();
} catch (IOException e) {
System.out.println("输入输出流异常");
e.printStackTrace();
}
showingInfo.append(
"\nconnecting "+server.getInetAddress().getHostName());

}
public void getStream(){
try {
os = new BufferedWriter( new OutputStreamWriter(server.getOutputStream()));
os.flush();
is = new BufferedReader( new InputStreamReader(server.getInputStream()));
showingInfo.append("\n got I/O streams");
} catch (IOException e) {
System.out.println("输入输出流异常");
e.printStackTrace();
}
}
public void processConnection() {
String message = "SERVER>>> Connection successful";

try {
os.write( message );
os.flush();
inputInfo.setEnabled(true);
do{

message =(String) is.readLine();
showingInfo.append("\n"+message);
showingInfo.setCaretPosition(showingInfo.getText().length());
}while(!message.equals("CLIENT>>>terminate"));
}
catch (IOException e) {
System.out.println("输入输出流异常");
e.printStackTrace();
}

}
public void close() {
showingInfo.append("\nconnection terminate");
inputInfo.setEnabled(false);
try {
is.close();
os.close();
server.close();
} catch (IOException e) {
System.out.println("输入输出流异常");
e.printStackTrace();
}

}
}

不明白为什么用Buffered就无法发送数据...而且运行时也不报错...
头痛...

求各位大侠帮我问题所在...谢谢了...
...全文
153 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
huoyijie 2007-08-14
  • 打赏
  • 举报
回复
呵呵 大家要互相帮助麽
KeroroCatboy 2007-08-14
  • 打赏
  • 举报
回复
I LOVE YOU...MAN....
一言惊醒梦中人....
非常感谢啊...
能有耐心看完这么长段代码...
真的谢谢了...T T 感动ing...
huoyijie 2007-08-14
  • 打赏
  • 举报
回复
代码已经调试过 应该不会有问题 呵呵 不过lz有些抠门啊才5分
服务器端:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
public class TestServer extends JFrame{

ServerSocket serverSocket;
Socket server;
BufferedReader is;
BufferedWriter os;

JTextArea showingInfo;
JTextField inputInfo;
int count = 1;


public TestServer(){

Container container = getContentPane();

inputInfo = new JTextField();
inputInfo.setEnabled(false);
inputInfo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
sendData(e.getActionCommand());
inputInfo.setText("");
}
});
container.add(inputInfo);
showingInfo = new JTextArea();
container.add(new JScrollPane(showingInfo),BorderLayout.CENTER);
container.add(inputInfo,BorderLayout.NORTH);
setSize(400,300);
setTitle("Server");
setVisible(true);
}
public static void main(String[] args) {
TestServer server = new TestServer();
server.setDefaultCloseOperation(server.EXIT_ON_CLOSE);
server.run();

}
public void sendData(String message) {
try {
os.write("SERVER>>>"+message+"\n");
os.flush();
} catch (IOException e) {
System.out.println("输入输出流异常");
e.printStackTrace();
}
showingInfo.append("\nSERVER>>>"+message);

}
public void run(){
try {
serverSocket = new ServerSocket(5000,10);
while(true){
connectTOClient();
getStream();
processConnection();
close();
count++;
}

} catch (IOException e) {
System.out.println("输入输出流异常");
e.printStackTrace();
}

}
public void connectTOClient() {

showingInfo.setText("waiting for connection");
try {
server = serverSocket.accept();
} catch (IOException e) {
System.out.println("输入输出流异常");
e.printStackTrace();
}
showingInfo.append(
"\nconnecting "+server.getInetAddress().getHostName());

}
public void getStream(){
try {
os = new BufferedWriter( new OutputStreamWriter(server.getOutputStream()));
os.flush();
is = new BufferedReader( new InputStreamReader(server.getInputStream()));
showingInfo.append("\n got I/O streams");
} catch (IOException e) {
System.out.println("输入输出流异常");
e.printStackTrace();
}
}
public void processConnection() {
String message = "SERVER>>> Connection successful";

try {
os.write( message +"\n");///////////1
os.flush();
inputInfo.setEnabled(true);
do{

message =(String) is.readLine();
showingInfo.append("\n"+message);
showingInfo.setCaretPosition(showingInfo.getText().length());

}while(!message.equals("CLIENT>>>terminate"));
}
catch (IOException e) {
System.out.println("输入输出流异常");
e.printStackTrace();
}

}
public void close() {
showingInfo.append("\nconnection terminate");
inputInfo.setEnabled(false);
try {
is.close();
os.close();
server.close();
} catch (IOException e) {
System.out.println("输入输出流异常");
e.printStackTrace();
}

}
}
客户端:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
public class TestClient extends JFrame {
String chatServer="";
Socket client;
BufferedReader is;
BufferedWriter os;


JTextArea showingInfo;
JTextField inputInfo;
public TestClient(String host){
chatServer = host;
Container container = getContentPane();
inputInfo = new JTextField();
inputInfo.setEnabled(false);
inputInfo.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent e){
sendData(e.getActionCommand());
inputInfo.setText("");
}

});

container.add(inputInfo,java.awt.BorderLayout.NORTH);
showingInfo = new JTextArea();
container.add(new JScrollPane(showingInfo),BorderLayout.CENTER);
setSize(400,300);
setTitle("Client");
setVisible(true);
}

public void run(){
connectToServer();
getStream();
processConnect();
close();

}
public static void main(String[] args) {
TestClient client;
if (args.length == 0){

client = new TestClient("127.0.0.1");
}
else{
client = new TestClient(args[0]);
}
client.setDefaultCloseOperation(client.EXIT_ON_CLOSE);
client.run();
}

public void connectToServer(){
showingInfo.setText("connection...\n");
try {
client = new Socket(InetAddress.getByName(chatServer),5000);
showingInfo.append(
"Connecting to "+client.getInetAddress().getHostName());
} catch (UnknownHostException e) {

System.out.println("不知名主机.");
} catch (IOException e) {
System.out.println("输入输出流异常");
e.printStackTrace();
}

}
public void getStream(){

try {
os = new BufferedWriter( new OutputStreamWriter(client.getOutputStream()));
os.flush();
is = new BufferedReader (new InputStreamReader(client.getInputStream()));
showingInfo.append("\ngot I/O Streams");

} catch (IOException e) {
System.out.println("输入输出流异常");
e.printStackTrace();
}
}
public void processConnect(){
inputInfo.setEnabled(true);
String message;
try {

do{
message = (String) is.readLine();

showingInfo.append("\n"+message);
showingInfo.setCaretPosition(showingInfo.getText().length());
}while(! message.equals("SERVER>>>terminate"));


} catch (EOFException e){
System.out.println("EOFError");
} catch (IOException e) {
System.out.println("输入输出流异常");
e.printStackTrace();
}
}
public void sendData(String message){
try {
os.write("CLIENT>>>"+message+"\n");
os.flush();
showingInfo.append("\nCLIENT>>>"+message);

} catch (IOException e) {
System.out.println("输入输出流异常");
e.printStackTrace();
}

}
public void close(){
showingInfo.append("\nconnection terminate");
inputInfo.setEnabled(false);
try {
is.close();
os.close();
client.close();
} catch (IOException e) {
System.out.println("输入输出流异常");
e.printStackTrace();
}


}
}
改动之处:
public void sendData(String message) {
try {
os.write("SERVER>>>"+message+"\n");
os.flush();
} catch (IOException e) {
System.out.println("输入输出流异常");
e.printStackTrace();
}
showingInfo.append("\nSERVER>>>"+message);

}中的这句
os.write("SERVER>>>"+message+"\n");加了一个“/n”
processConnection()方法中os.write( message +"\n");加了一个“/n”
总结:
is.readLine()方法读一行是以"\n"为界的
所以没有遇到"\n" 这个方法是不返回的

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧