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

为什么我的socket连接上了,但不能向服务端发送数据
Socket socket = new Socket("127.0.0.1", 8001);
  // 向服务端程序发送数据
  OutputStream ops = socket.getOutputStream();  
  OutputStreamWriter opsw = new OutputStreamWriter(ops);
  BufferedWriter bw = new BufferedWriter(opsw);
   
  bw.write("hello world\r\n\r\n");
  bw.flush();
上面是客户端程序
为什么我的socket连接上了,但不能向服务端发送数据

------解决方案--------------------
服务器端有没有读取?
------解决方案--------------------
Java code

package Ichat;
import java.io.*;
import java.net.*;
import java.util.*;
public class Client {
    private String name;
    private Socket s;
    private ReadThread rt;
    private DataOutputStream dos;
    public Client(){
        try {
            s= new Socket("192.168.1.105",8889);
        }  catch (Exception e) {
            e.printStackTrace();
        }
    }
    public Client(Socket s){
        this.s=s;
    }
    public void chat(){
        System.out.println("在开始聊天之前请输入您的姓名:");
        Scanner sc = new Scanner(System.in);
        this.name = sc.next();
        try{
            dos= new DataOutputStream(s.getOutputStream());
            rt = new ReadThread(s);
            rt.start();
            System.out.println("连接成功,现在您可以开始聊天了:");
            while(true){
                String msg = sc.next();
                if("byebye".equals(msg)){
                    s.close();
                    dos.close();
                    System.out.println("连接已经断开...");
                    System.exit(0);
                }
                dos.writeUTF(name+"说: "+msg);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        new Client().chat();
    }
}

------解决方案--------------------
探讨
我用tcp工具为什么好使,他是怎么发送数据的啊