java做的 socket服务器
客户端是别的语言编写怎么才能让客户端连接上服务器时
服务器自动给客户端发送一个信息,例如55 C0 A8 01 01 30 30 30 30 30 33 20 30 30 31 32 33
下边是我的代码,我需要怎么修改?哪些是没用的代码?
public class EchoServer
{
     public static void main(String[] args)
     {
         try{
             ServerSocket s = new ServerSocket(5050);
             Socket incoming = s.accept();
             
             BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
             PrintWriter out = new PrintWriter(incoming.getOutputStream(), true /* autoFlush */);             out.println("55 C0 A8 01 01 30 30 30 30 30 33 20 30 30 31 32 33");             
             
             boolean done = false;
             while (!done)
             {
                 String line = in.readLine();
                 if (line == null)
                     done = true;
                 else{
                     out.println("You to Server : " + line);
                     System.out.println("Client to you :"+line);
                     if (line.trim().equals("BYE"))
                         done = true;
                 }             }             incoming.close();
         }
         catch (Exception e){
             System.out.println(e);
         }
     }}

解决方案 »

  1.   

    我用java写了个测试的客户端,如果说这个能接收到数据
    是不是就说明服务器端的发送没问题?mport java.io.*;
    import java.net.*;public class EchoClient {     public static void main(String args[]) {
             try {          Socket connection =new Socket("127.0.0.1", 5050);
              BufferedReader input = new BufferedReader(new InputStreamReader(
                        connection.getInputStream()));
                 
                 PrintWriter out = new PrintWriter(connection.getOutputStream(),true );
                 String info;
                 info = input.readLine();
                 System.out.println(info);
                 boolean done = false;
                 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                 String sInput;
                 
                 //out.println("BYE");             while (!done) {
                                   info = input.readLine();
                     System.out.println(info);
                     
                 }
                 connection.close();
             }
             catch (SecurityException e) {
                 System.out.println("SecurityException when connecting Server!");
             }
             catch (IOException e) {
                 System.out.println("IOException when connecting Server!");
             }
         }}
      

  2.   

    是的,不过你最好输出的时候再flush一下,感觉是因为没有flush的原因
      

  3.   

    import java.io.*;
    import java.net.*;
    import java.util.*;public class ChatServer {
    boolean started = false;
    ServerSocket ss = null;

    List<Client> clients = new ArrayList<Client>();

    public static void main(String[] args) {
    new ChatServer().start();
    }

    public void start() {
    try {
    ss = new ServerSocket(8888);
    started = true;
    } catch (BindException e) {
    System.out.println("端口使用中....");
    System.out.println("请关掉相关程序并重新运行服务器!");
    System.exit(0);
    } catch (IOException e) {
    e.printStackTrace();
    }

    try {

    while(started) {
    Socket s = ss.accept();
    Client c = new Client(s);
    System.out.println("a client connected!");
    new Thread(c).start();
    clients.add(c);
    //dis.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    ss.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

    class Client implements Runnable {
    private Socket s;
    private DataInputStream dis = null;
    private DataOutputStream dos = null;
    private boolean bConnected = false;

    public Client(Socket s) {
    this.s = s;
    try {
    dis = new DataInputStream(s.getInputStream());
    dos = new DataOutputStream(s.getOutputStream());
    bConnected = true;
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    public void send(String str) {
    try {
    dos.writeUTF(str);
    } catch (IOException e) {
    clients.remove(this);
    System.out.println("对方退出了!我从List里面去掉了!");
    //e.printStackTrace();
    }
    }

    public void run() {
    try {
    while(bConnected) {
    String str = dis.readUTF();
    System.out.println(str);
    for(int i=0; i<clients.size(); i++) {
    Client c = clients.get(i);
    c.send(str);
    //System.out.println(" a string send !");
    }
    /*
    for(Iterator<Client> it = clients.iterator(); it.hasNext(); ) {
    Client c = it.next();
    c.send(str);
    }
    */
    /*
    Iterator<Client> it = clients.iterator();
    while(it.hasNext()) {
    Client c = it.next();
    c.send(str);
    }
    */
    }
    } catch (EOFException e) {
    System.out.println("Client closed!");
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    if(dis != null) dis.close();
    if(dos != null) dos.close();
    if(s != null)  {
    s.close();
    //s = null;
    }

    } catch (IOException e1) {
    e1.printStackTrace();
    }


    }
    }

    }
    }
      

  4.   

    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.net.*;public class ChatClient extends Frame {
    Socket s = null;
    DataOutputStream dos = null;
    DataInputStream dis = null;
    private boolean bConnected = false; TextField tfTxt = new TextField(); TextArea taContent = new TextArea();

    Thread tRecv = new Thread(new RecvThread());  public static void main(String[] args) {
    new ChatClient().launchFrame(); 
    } public void launchFrame() {
    setLocation(400, 300);
    this.setSize(300, 300);
    add(tfTxt, BorderLayout.SOUTH);
    add(taContent, BorderLayout.NORTH);
    pack();
    this.addWindowListener(new WindowAdapter() { @Override
    public void windowClosing(WindowEvent arg0) {
    disconnect();
    System.exit(0);
    }

    });
    tfTxt.addActionListener(new TFListener());
    setVisible(true);
    connect();

    tRecv.start();
    }

    public void connect() {
    try {
    s = new Socket("127.0.0.1", 8888);
    dos = new DataOutputStream(s.getOutputStream());
    dis = new DataInputStream(s.getInputStream());
    System.out.println("connected!");
    bConnected = true;
    } catch (UnknownHostException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }

    }

    public void disconnect() {
    try {
    dos.close();
    dis.close();
    s.close();
    } catch (IOException e) {
    e.printStackTrace();
    }

    /*
    try {
    bConnected = false;
    tRecv.join();
    } catch(InterruptedException e) {
    e.printStackTrace();
    } finally {
    try {
    dos.close();
    dis.close();
    s.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    */
    }

    private class TFListener implements ActionListener { public void actionPerformed(ActionEvent e) {
    String str = tfTxt.getText().trim();
    //taContent.setText(str);
    tfTxt.setText("");

    try {
    //System.out.println(s);
    dos.writeUTF(str);
    dos.flush();
    //dos.close();
    } catch (IOException e1) {
    e1.printStackTrace();
    }

    }

    }

    private class RecvThread implements Runnable { public void run() {
    try {
    while(bConnected) {
    String str = dis.readUTF();
    //System.out.println(str);
    taContent.setText(taContent.getText() + str + '\n');
    }
    } catch (SocketException e) {
    System.out.println("退出了,bye!");
    } catch (EOFException e) {
    System.out.println("推出了,bye - bye!");
    } catch (IOException e) {
    e.printStackTrace();


    }

    }
    }
    这是尚学堂的一个小例子,你看看。主动发消息,这没实现
      

  5.   

    我把那点改为out.println("55 C0 A8 01 01 30 30 30 30 30 33 20 30 30 31 32 33");
    incoming.getOutputStream().flush(); 还是不行
      

  6.   

    当然不行,你要flush的是out啊
      

  7.   

    我也是初学者,我曾经编写过类似的程序。
    我当时就是指定服务器的端口,然后通过write方法写入信息
    程序的一部分,希望对你有启发
                               String hostname = strServer;
    int port = 80;
    InetAddress addr = InetAddress.getByName(hostname);//在给定主机名的情况下确定主机的 IP 地址
    Socket socket = new Socket(addr, port); // 建立一个Socket
    socket.setSoTimeout(30000);
    // 发送命令
    BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(
    socket.getOutputStream()));
    StringBuffer PostData = new StringBuffer();

    String request=PostData.toString(); wr.write("POST " + strPage + " HTTP/1.1\r\n");
    wr.write("Host: " + strServer + ":" + port + "\r\n");
    wr.write("Content-Length: " + PostData.length() + "\r\n");
    wr.write("Content-Type: application/x-www-form-urlencoded\r\n");
    wr.write("\r\n");
    wr.write(request);
    wr.flush();
    // 接收返回的结果
      

  8.   

    to   yuwenbao 郁闷,还是不行
      

  9.   

    我用的eclipse
    我用java写的客户端接收没问题
    就是不能和另外语言写的通信
      

  10.   

    http://blog.csdn.net/weixing979/archive/2006/05/12/725940.aspx
    你看这篇文章说c++和java通信都要先把数据进行字节序转换,你是用c++么,会不会是同样的问题。
      

  11.   

    程序应该是没有问题,与其他语言写的客户端通信不要用Writer,直接向stream中写二进制。
      

  12.   

    不要用PrinterWriter,改成DataOutputStream试试,别的语言可能不支持字符传输
      

  13.   

    二进制流和PrinterWriter写出的数据没什么区别