日期:2014-05-20  浏览次数:20661 次

大家帮忙看看这段聊天程序
import   java.io.*;
import   java.net.*;
public   class   TestClient    
{
public   static   void   main(String[]   args)  
{
try
{
Socket   socket   =   new   Socket( "192.168.0.13 ",8000);

System.out.println( "服务IP地址---> "+socket);


InputStream   input   =   socket.getInputStream();

OutputStream   output   =     socket.getOutputStream();

BufferedReader   buffer   =   new   BufferedReader(new   InputStreamReader(input));

PrintWriter   print   =   new   PrintWriter(output);

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

String   s   =   buffer_line.readLine();

while(true)
{
print.println(s);//把通过键盘输入的信息给服务端..

System.out.println( "客户端来的信息 "+s);

print.flush();//立刻给服务端

if   (s.equals( "quit "))
{
break;
}

System.out.println( "服务端发来的信息 "+buffer.readLine());

s   =   buffer_line.readLine();



}


buffer.close();
print.close();
socket.close();
}

catch(Exception   e)
{
e.printStackTrace();
}

}
}


import   java.io.*;
import   java.net.*;
public   class   TestServer  
{
public   static   void   main(String[]   args)  
{
try
{
ServerSocket   server     =   new   ServerSocket(8000);

Socket   socket   =   server.accept();

System.out.println( "kehuIP地址---> "   +   server);

InputStream   input   =   socket.getInputStream();

OutputStream   output   =     socket.getOutputStream();

BufferedReader   buffer   =   new   BufferedReader(new   InputStreamReader(input));

PrintWriter   print   =   new   PrintWriter(output);

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

String   s   =   buffer_line.readLine();

while(true)
{
print.println(s);//把通过键盘输入的信息给服务端..

System.out.println( "服务端发来的信息 "+s);

print.flush();//立刻给服务端

if   (s.equals( "quit "))
{
break;
}

System.out.println( "客户发来的信息 "   +   buffer_line.readLine());

s   =   buffer_line.readLine();


 
buffer.close();
print.close();
}


socket.close();
}

catch(Exception   e)
{
e.printStackTrace();
}

}
}


有点不太明白getInputStream   getOutputStream

是不是客户端调用getOutputStream就是把客户端键盘输入的消息给服务端

那getInputStream又是怎么回事...
大家可运行一下我的程序

怎么服务端没有收到客户端的消息

有帮忙改的请加上点注释...


------解决方案--------------------
inputstream是获得的
outputstream是输出的
------解决方案--------------------