首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

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

2012-01-02 
java聊天程序的问题..请大侠们帮帮小弟...一个Client客户端,一个Server服务器端...在数据输入及输出的时候

java聊天程序的问题..请大侠们帮帮小弟...
一个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就无法发送数据...而且运行时也不报错...
头痛...

求各位大侠帮我问题所在...谢谢了...

[解决办法]
代码已经调试过 应该不会有问题 呵呵 不过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 " 这个方法是不返回的

热点排行