siwalet 2011-05-13 16:58
浏览 709
已采纳

socket服务端可以接受消息但是无法返回的问题

这是服务端代码
[code="java"]
package com.hj.demo.socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
public Server() throws IOException {
ServerSocket ss = new ServerSocket(7777);
while (true) {
Socket sk = ss.accept();
ClientThread ct = new ClientThread(sk);
ct.start();
System.out.println("服务端已启动...");
}

}

// 多线程客户端
class ClientThread extends Thread {
    private Socket clientSocket = null;

    public ClientThread(Socket clientSocket) {
        this.clientSocket = clientSocket;
    }
    DataInputStream dis = new DataInputStream(null);
    @SuppressWarnings("deprecation")
    @Override
    public void run() {
        // TODO Auto-generated method stub

        String hostName = clientSocket.getInetAddress().toString();
        System.out.println("hostName:>" + hostName+"已连接");
        String msg = null;
        try {
            dis = new DataInputStream(clientSocket
                    .getInputStream());
            while (true) {
                msg =  dis.readUTF();
                System.out.println(hostName + "发来的消息>: " + msg);
                if(null==dis.readLine()||"".equals(dis.readLine())){
                    break;
                }

            }
            if(!"".equals(msg)&&msg.length()!=0){

                DataOutputStream dos = new DataOutputStream(clientSocket.getOutputStream());
                String rep = "我是返回给由"+hostName+"发来["+msg+"]的消息";
                System.out.println(rep);
                dos.writeUTF(rep);
                dos.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        super.run();
    }

}

public static void main(String[] args) throws IOException {
    new Server();

}

}
[/code]

这是客户端的代码
[code="java"]
package com.hj.demo.socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {

public static void main(String[] args) throws UnknownHostException,
        IOException, InterruptedException {

    Socket sk = new Socket("127.0.0.1", 7777);
    DataOutputStream dos = new DataOutputStream(sk.getOutputStream());
    DataInputStream dis = new DataInputStream(sk.getInputStream());
    dos.writeUTF("hello,world");
    String rep  =  dis.readUTF();
    System.out.println("主机返回的消息>: " + rep);
        dos.close();
        dis.close();
}

}

[/code]

服务端可以接收发来的消息,但是返回就不行,求解答!

  • 写回答

6条回答

  • qianzhi008 2011-05-13 17:05
    关注

    [code="java"]
    if(!"".equals(msg)&&msg.length()!=0){

                    DataOutputStream dos = new DataOutputStream(clientSocket.getOutputStream());  
                    String rep = "我是返回给由"+hostName+"发来["+msg+"]的消息";  
                    System.out.println(rep);  
                    dos.writeUTF(rep);  
                    [color=red]dos.flush(); [/color] 
                    dos.close();  
                }  
    

    [/code]
    这边要调用一下flush方法,刷新一下,让服务器端把数据发送出去。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
  • ¥50 invest生境质量模块
  • ¥15 nhanes加权logistic回归,svyglm函数