import java.net.*;
import java.io.*;
public class TalkClient
{ public static void main(String args[])
{
Socket client = null;
try
{
client = new Socket("127.0.0.1",8888);
}
catch (IOException e)
{
System.out.println("连接不成功!"+e);
}
try
{
DataInputStream inline = new DataInputStream(System.in);
DataOutputStream out = new DataOutputStream(client.getOutputStream());
DataInputStream  in = new DataInputStream(client.getInputStream());
String s = null ;
s = inline.readUTF();

while (!(s.equals("bye")))
{  
System.out.println(s);
out.writeUTF(s);
System.out.println("服务器说:"+in.readUTF());
s = inline.readUTF();
}
inline.close();
out.close();
in.close();
client.close();

}
catch (IOException e)
{
System.out.println(e);
}
}

 import java.net.*;
import java.io.*;public class TalkServer
{
public static void main(String[] args)
{
ServerSocket server = null ;
try
{
server = new ServerSocket(8888);
}
catch (IOException e)
{
System.out.println("不能监听:"+e);
System.exit(-1);
}
Socket client = null;
try
{
client = server.accept();
System.out.println("客户端连接了!");
}
catch (IOException e)
{
System.out.println("客户端断开连接!"+e);
System.exit(-1);
}
try
{
DataInputStream in = new DataInputStream(client.getInputStream());
DataOutputStream out = new DataOutputStream(client.getOutputStream());
String line = null;
DataInputStream inline = new DataInputStream(System.in);
System.out.println("客户端说:"+in.readUTF());
line = inline.readUTF();
while (!(line.equals("bye")))
{
out.writeUTF(line);
out.flush();

line = inline.readUTF();

}
in.close();
out.close();
inline.close();
client.close();

}
catch (IOException e)
{
            System.out.println(e);
} }

服务器开启了,客服端连接上了,但客户端发不了信息出去,是不是我用的流不正确啊?请高手帮我看看,谢谢
 

解决方案 »

  1.   

    out.flush()要清空缓存,将流上的数据强制发出.
      

  2.   

    答:流使用得不大好。能正确运行的修改后的代码如下:请你自己运行一下参考吧。import java.net.*; 
    import java.io.*; public class TalkServer 

    public static void main(String[] args) 

    ServerSocket server = null ; 
    try 

    server = new ServerSocket(8888); 

    catch (IOException e) 

    System.out.println("不能监听:"+e); 
    System.exit(-1); 

    Socket client = null; 
    try 

    client = server.accept(); 
    System.out.println("客户端连接了!"); 

    catch (IOException e) 

    System.out.println("客户端断开连接!"+e); 
    System.exit(-1); 

    try 

    BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); 
    PrintWriter out = new PrintWriter(client.getOutputStream()); 
    String line = null; 
    BufferedReader inline = new BufferedReader(new InputStreamReader(System.in)); 
    System.out.println("客户端说:"+in.readLine()); 
    line = inline.readLine(); 
    while (!(line.equals("bye"))) 

    out.println(line); 
    out.flush(); line = inline.readLine(); } 
    in.close(); 
    out.close(); 
    inline.close(); 
    client.close(); } 
    catch (IOException e) 

                System.out.println(e); 
    } } 
    }  
    import java.net.*; 
    import java.io.*; 
    public class TalkClient 
    { public static void main(String args[]) 

    Socket client = null; 
    try 

    client = new Socket("127.0.0.1",8888); 

    catch (IOException e) 

    System.out.println("连接不成功!"+e); 

    try 

    BufferedReader inline = new BufferedReader(new InputStreamReader(System.in)); 
    BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); 
    PrintWriter out = new PrintWriter(client.getOutputStream()); 
    String s = null ; 
    s = inline.readLine(); while (!(s.equals("bye"))) 
    {   
    System.out.println(s); 
    out.println(s); 
    out.flush();
    System.out.println("服务器说:"+in.readLine()); 
    s = inline.readLine();

    inline.close(); 
    out.close(); 
    in.close(); 
    client.close(); } 
    catch (IOException e) 

    System.out.println(e); 


    }  
      

  3.   

    一般处理聊天这些方面的时候,是不是都是用bufferedReader,printwriter这2个流的啊?还有其他的流可以用吗?用那些流最好?
      

  4.   

    用ByteArrayOutputStream
    Socket client  = ServerSock.accept();
    InputStream Input = client.getInputStream();
    OutputStream Output= client.getOutputStream();
    ......
    ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
    while( Input.available() > 0 )
      bytestream.write(Input.read());
    byte[] bytes = bytestream.toByteArray();
    String strFromClient = new String(bytes);
    System.out.println("From client: "+strFromClient);
    String result = "Feedback from Server";
    Output.write(result.getBytes());
    Output.flush();
    Output.close();
    Input.close();
    ComSocket.close();