maxcion 2017-01-16 04:17 采纳率: 100%
浏览 945
已采纳

Java Socket通讯简单demo遇到的问题,求大神帮助

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class TcpIpTest {

public static void main(String[] args) {
    int i = 9090;
    String s = new String("127.0.0.1");

// String content = new String("hello,I am client");
Client client = new Client(s,i);
// Client client = new Client(s, content, i);
Server server = new Server(i);
//System.out.println("11111111111111111111");
server.start();
client.start();
//System.out.println("211111111111111111111");

}

}

class Client {

String str = null;
String content;
int i;

Socket socket = null;
OutputStream os = null;

// Client(String str, String content, int i) {
// this.str = str;
// this.content = content;
// this.i = i;
//
// }

Client(String str, int i) {
    this.str = str;
    // this.content=content;
    this.i = i;
}

void start() {
    try {
        socket = new Socket(InetAddress.getByName(str),i);
        os = socket.getOutputStream();
        os.write(str.getBytes());
        socket.shutdownOutput();
    } catch (UnknownHostException e) {

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

        e.printStackTrace();
    } finally {
        try {
            if (os != null)
                os.close();
        } catch (IOException e1) {

            e1.printStackTrace();
        }
        try {
            if (socket != null)
                socket.close();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
}

}

class Server {
int i;
ServerSocket ss=null;
InputStream is = null;
Socket s = null;

Server(int i) {
    this.i = i;

}

void start() {
    try {
        ss = new ServerSocket(i);

        s = ss.accept();
        // ss.setSoTimeout(2500);
        is = s.getInputStream();
        int len ;
        byte[] b = new byte[20];
        while ((len = is.read(b)) != -1) {
            String s1 = new String(b, 0, len);
            System.out.println(s1);
        }
    } catch (IOException e) {

        e.printStackTrace();
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (s != null) {
            try {
                s.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
        }
        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
        }
    }

}

}


无法进行通讯
下面是日志截图![图片说明](https://img-ask.csdn.net/upload/201701/16/1484540208_603025.png)图片说明

  • 写回答

2条回答

  • 有头发的猿 2017-01-16 05:32
    关注

    很明显你的服务端已经写好了,客户端可能有点问题,我看你的代码我只想说一句垃圾代码。看的太费劲了

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

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊