繁体   English   中英

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

[英]Unable to send data from my server to client

我制作了一个简单的聊天程序,其中从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();}
}

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

无论如何。 如果要像聊天程序一样从服务器向客户端发送消息,则代码应如下所示:

服务器部分:

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();
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM