• 大小: 9.97MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-10-16
  • 语言: Java
  • 标签: Java  Socket  通信  服务  

资源简介

完整的Java Socket 通信服务与客户端完整示例,包含workspace整个工程,可用于学习调试必备

资源截图

代码片段和文件信息

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/*
 * 基于TCP协议的Socket通信,实现用户登陆
 * 服务器端
 */
public class Server {
    public static void main(String[] args) {
        try {
            //1.创建一个服务器端Socket,即ServerSocket,指定绑定的端口,并监听此端口
            ServerSocket serverSocket=new ServerSocket(8888);
            Socket socket=null;
            //记录客户端的数量
            int count=0;
            System.out.println(“***服务器即将启动,等待客户端的连接***“);
            //循环监听等待客户端的连接
            while(true){
                //调用accept()方法开始监听,等待客户端的连接
                socket=serverSocket.accept();
                //创建一个新的线程
                ServerThread serverThread=new ServerThread(socket);
                //启动线程
                serverThread.start();

                count++;//统计客户端的数量
                System.out.println(“客户端的数量:“+count);
                InetAddress address=socket.getInetAddress();
                System.out.println(“当前客户端的IP:“+address.getHostAddress());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-10-29 09:21  JavaSocketworkspace\
     目录           0  2017-10-29 09:21  JavaSocketworkspace\workspace\
     目录           0  2017-10-29 09:21  JavaSocketworkspace\workspace\.metadata\
     文件           0  2017-03-16 19:37  JavaSocketworkspace\workspace\.metadata\.lock
     文件       78321  2017-03-16 20:06  JavaSocketworkspace\workspace\.metadata\.log
     目录           0  2017-10-29 09:21  JavaSocketworkspace\workspace\.metadata\.mylyn\
     目录           0  2017-10-29 09:21  JavaSocketworkspace\workspace\.metadata\.mylyn\.taskListIndex\
     文件          20  2017-03-16 19:38  JavaSocketworkspace\workspace\.metadata\.mylyn\.taskListIndex\segments.gen
     文件          32  2017-03-16 19:38  JavaSocketworkspace\workspace\.metadata\.mylyn\.taskListIndex\segments_1
     文件         235  2017-03-16 20:00  JavaSocketworkspace\workspace\.metadata\.mylyn\.tasks.xml.zip
     目录           0  2017-10-29 09:21  JavaSocketworkspace\workspace\.metadata\.mylyn\contexts\
     文件         561  2017-03-16 19:39  JavaSocketworkspace\workspace\.metadata\.mylyn\repositories.xml.zip
     文件         235  2017-03-16 20:08  JavaSocketworkspace\workspace\.metadata\.mylyn\tasks.xml.zip
     目录           0  2017-10-29 09:21  JavaSocketworkspace\workspace\.metadata\.plugins\
     目录           0  2017-10-29 09:21  JavaSocketworkspace\workspace\.metadata\.plugins\org.eclipse.core.resources\
     目录           0  2017-10-29 09:21  JavaSocketworkspace\workspace\.metadata\.plugins\org.eclipse.core.resources\.history\
     目录           0  2017-10-29 09:21  JavaSocketworkspace\workspace\.metadata\.plugins\org.eclipse.core.resources\.history\34\
     文件           0  2017-03-16 19:41  JavaSocketworkspace\workspace\.metadata\.plugins\org.eclipse.core.resources\.history\34\d0273a7f3d0a00171a4bb6e900236990
     目录           0  2017-10-29 09:21  JavaSocketworkspace\workspace\.metadata\.plugins\org.eclipse.core.resources\.history\6b\
     文件          30  2017-03-16 19:44  JavaSocketworkspace\workspace\.metadata\.plugins\org.eclipse.core.resources\.history\6b\e06c9a003e0a00171a4bb6e900236990
     目录           0  2017-10-29 09:21  JavaSocketworkspace\workspace\.metadata\.plugins\org.eclipse.core.resources\.history\8b\
     文件         446  2017-03-16 19:43  JavaSocketworkspace\workspace\.metadata\.plugins\org.eclipse.core.resources\.history\8b\3026ccfa3f0a00171a4bb6e900236990
     目录           0  2017-10-29 09:21  JavaSocketworkspace\workspace\.metadata\.plugins\org.eclipse.core.resources\.history\9d\
     文件          36  2017-03-16 20:01  JavaSocketworkspace\workspace\.metadata\.plugins\org.eclipse.core.resources\.history\9d\20560a4c400a00171a4bb6e900236990
     目录           0  2017-10-29 09:21  JavaSocketworkspace\workspace\.metadata\.plugins\org.eclipse.core.resources\.history\b5\
     文件           0  2017-03-16 20:01  JavaSocketworkspace\workspace\.metadata\.plugins\org.eclipse.core.resources\.history\b5\702fa339400a00171a4bb6e900236990
     目录           0  2017-10-29 09:21  JavaSocketworkspace\workspace\.metadata\.plugins\org.eclipse.core.resources\.history\c3\
     文件         882  2017-03-16 19:45  JavaSocketworkspace\workspace\.metadata\.plugins\org.eclipse.core.resources\.history\c3\a0436e48400a00171a4bb6e900236990
     目录           0  2017-10-29 09:21  JavaSocketworkspace\workspace\.metadata\.plugins\org.eclipse.core.resources\.history\de\
     文件         364  2017-03-16 19:38  JavaSocketworkspace\workspace\.metadata\.plugins\org.eclipse.core.resources\.history\de\00a0651a3d0a00171a4bb6e900236990
     目录           0  2017-10-29 09:21  JavaSocketworkspace\workspace\.metadata\.plugins\org.eclipse.core.resources\.history\e9\
............此处省略152个文件信息

评论

共有 条评论