无法将数据从我的服务器发送到客户端

时间:2013-12-26 20:34:57

标签: java

我制作了一个简单的聊天程序,从MyClient向MyServer发送数据正在运行,但是从MyServer向MyClient发送数据不起作用。

那么我在哪里犯错了?

这是MyServer程序:

import java.io.*;

import java.net.*;

public class MyServer{

    ServerSocket ss;
       Socket s;
        DataInputStream din;
        DataOutputStream dout;


public MyServer(){

    try{

        System.out.println("Server START......");

            ss=new ServerSocket(9000);
            s=ss.accept();

            System.out.println("Client Connected.....");

            din=new DataInputStream(s.getInputStream());
            dout=new DataOutputStream(s.getOutputStream());
                chat();
        }
    catch(Exception e){
        System.out.println(e);}
}

public void chat()throws IOException{

    String str=" ";

        do{
            str=din.readUTF();

            System.out.println("Client Message: "+str);

            dout.writeUTF("I have recieved ur message:"+str);
            dout.flush();

            }while(!str.equals("stop"));
}

public static void main(String arg[]){

    new MyServer();}
}

这是MyClient程序:

import java.io.*;

import java.net.*;

public class MyClient{

    Socket s;
        DataInputStream din;
        DataOutputStream dout;

public MyClient(){

    try{
        s=new Socket("localhost",9000);

        System.out.println(s);

            din=new DataInputStream(s.getInputStream());
            dout=new DataOutputStream(s.getOutputStream());
                chat();

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

public void chat()throws IOException{

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

        String s1;

    do{
        s1=br.readLine();

        dout.writeUTF(s1);
        dout.flush();

        System.out.println("Server Message: "+din.readUTF());

        }while(!s1.equals("stop"));
}

public static void main(String arg[]){

    new MyClient();}
}

1 个答案:

答案 0 :(得分:0)

实际上,您已经通过“我收到了您的消息......”从服务器向客户发送信息......

反正。如果您想将消息从服​​务器发送到客户端,就像聊天程序一样,您的代码应如下所示:

服务器部分:

public class Server {

ServerSocket ss;
Socket s;
DataInputStream din;
DataOutputStream dout;

public Server() {

    try {

        System.out.println("Server START......");

        ss = new ServerSocket(9000);
        s = ss.accept();

        System.out.println("Client Connected.....");

        din = new DataInputStream(s.getInputStream());
        dout = new DataOutputStream(s.getOutputStream());
        chat();
    } catch (Exception e) {
        System.out.println(e);
    }
}

public void chat() throws IOException {

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

    String s1;

    do {

        System.out.println("Client Message: " + din.readUTF());

        s1=br.readLine();
        dout.writeUTF(s1);
        dout.flush();

    } while (!s1.equals("stop"));
}

public static void main(String arg[]) {

    new Server();
}

}

对于客户:

public class Client {

Socket s;
DataInputStream din;
DataOutputStream dout;

public Client() {

    try {
        s = new Socket("localhost", 9000);

        System.out.println(s);

        din = new DataInputStream(s.getInputStream());
        dout = new DataOutputStream(s.getOutputStream());
        chat();

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

public void chat() throws IOException {

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

    String s1;

    do {

        s1 = br.readLine();

        dout.writeUTF(s1);
        dout.flush();   

        System.out.println("Server Message: " + din.readUTF());

    } while (!s1.equals("stop"));
}

public static void main(String arg[]) {

    new Client();
}

}