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条)

报告相同问题?

悬赏问题

  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败