• 大小: 0.61M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-04
  • 语言: Java
  • 标签: 其他  

资源简介

JavaSocket建立连接和发送消息.zip

资源截图

代码片段和文件信息

package com.example.socketserver;

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

/**
 * 服务端
 *
 */
public class SocketServer{

public static void main(String[] args) {
startService();
}

/** 
 * 启动服务监听,等待客户端连接
 */
private static void startService() {
try {
// 创建ServerSocket
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println(“--开启服务器,监听端口 9999--“);

// 监听端口,等待客户端连接
while (true) {
System.out.println(“--等待客户端连接--“);
Socket socket = serverSocket.accept(); //等待客户端连接
System.out.println(“得到客户端连接:“ + socket);

startReader(socket);
}

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

/** 
 * 从参数的Socket里获取最新的消息
 */
private static void startReader(final Socket socket) {

new Thread(){
@Override
public void run() {
DataInputStream reader;
try {
// 获取读取流
reader = new DataInputStream( socket.getInputStream());
while (true) {
System.out.println(“*等待客户端输入*“);
// 读取数据
String msg = reader.readUTF();
System.out.println(“获取到客户端的信息:“ + msg);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}

}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件      324568  2016-04-25 20:45  效果图.gif
     目录           0  2016-04-25 20:56  ImSocketClient\
     文件         475  2016-04-25 20:22  ImSocketClient\.classpath
     文件         850  2016-04-25 20:25  ImSocketClient\.project
     目录           0  2016-04-25 20:56  ImSocketClient\.settings\
     文件          57  2016-04-25 20:22  ImSocketClient\.settings\org.eclipse.core.resources.prefs
     文件         177  2015-11-09 10:17  ImSocketClient\.settings\org.eclipse.jdt.core.prefs
     文件         955  2016-04-25 20:26  ImSocketClient\AndroidManifest.xml
     目录           0  2015-11-09 10:17  ImSocketClient\assets\
     目录           0  2016-04-25 20:56  ImSocketClient\bin\
     文件         955  2016-04-25 20:56  ImSocketClient\bin\AndroidManifest.xml
     目录           0  2016-04-25 20:56  ImSocketClient\bin\classes\
     目录           0  2016-04-25 20:56  ImSocketClient\bin\classes\com\
     目录           0  2016-04-25 20:56  ImSocketClient\bin\classes\com\example\
     目录           0  2016-04-25 20:56  ImSocketClient\bin\classes\com\example\socketclient\
     文件         357  2016-04-25 20:56  ImSocketClient\bin\classes\com\example\socketclient\BuildConfig.class
     文件        1470  2016-04-25 20:56  ImSocketClient\bin\classes\com\example\socketclient\MainActivity$1.class
     文件        1310  2016-04-25 20:56  ImSocketClient\bin\classes\com\example\socketclient\MainActivity$2.class
     文件        1273  2016-04-25 20:56  ImSocketClient\bin\classes\com\example\socketclient\MainActivity.class
     文件         358  2016-04-25 20:56  ImSocketClient\bin\classes\com\example\socketclient\R$attr.class
     文件         479  2016-04-25 20:56  ImSocketClient\bin\classes\com\example\socketclient\R$dimen.class
     文件         425  2016-04-25 20:56  ImSocketClient\bin\classes\com\example\socketclient\R$drawable.class
     文件         411  2016-04-25 20:56  ImSocketClient\bin\classes\com\example\socketclient\R$id.class
     文件         421  2016-04-25 20:56  ImSocketClient\bin\classes\com\example\socketclient\R$layout.class
     文件         406  2016-04-25 20:56  ImSocketClient\bin\classes\com\example\socketclient\R$menu.class
     文件         490  2016-04-25 20:56  ImSocketClient\bin\classes\com\example\socketclient\R$string.class
     文件         449  2016-04-25 20:56  ImSocketClient\bin\classes\com\example\socketclient\R$style.class
     文件         733  2016-04-25 20:56  ImSocketClient\bin\classes\com\example\socketclient\R.class
     目录           0  2016-04-25 20:56  ImSocketClient\bin\res\
     目录           0  2016-04-25 20:56  ImSocketClient\gen\
     目录           0  2016-04-25 20:56  ImSocketClient\gen\com\
............此处省略51个文件信息

评论

共有 条评论