首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TCP服务器小程序

TCP服务器小程序
EN

Stack Overflow用户
提问于 2013-02-07 14:52:29
回答 1查看 883关注 0票数 0

我有一个开放的社交容器,我需要在该容器中放置一个小程序,该小程序需要充当tcpServer,我需要一些指导,我该如何做?

EN

回答 1

Stack Overflow用户

发布于 2013-02-07 16:05:31

networking tutorial详细介绍了如何创建TCP服务器套接字并接受来自该套接字的连接。

这里是它的要点:

代码语言:javascript
复制
    ServerSocket serverSocket = null;
    try {
        serverSocket = new ServerSocket(4444);
    } catch (IOException e) {
        System.err.println("Could not listen on port: 4444.");
        System.exit(1);
    }

    Socket clientSocket = null;
    try {
        clientSocket = serverSocket.accept();
    } catch (IOException e) {
        System.err.println("Accept failed.");
        System.exit(1);
    }

    // now get the input and output streams from the client socket
    // and use them  to read and write to the TCP connection

    clientSocket.close();
    serverSocket.close();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14745318

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档