转载请标明出处:http://blog.csdn.net/xx326664162/article/details/51752701 文章出自:薛瑄的博客
你也可以查看我的其他同类文章,也会让你有一定的收货!
Socket简单例子
服务端:
public class Server { public static final int PORT = 12345;//监听的端口号 public static void main(String[] args) {// System.out.println("服务器启动...\n"); System.out.println("server start...\n"); Server server = new Server(); server.init(); } public void init() { try { ServerSocket serverSocket = new ServerSocket(PORT); while (true) { // 一旦有堵塞, 则表示服务器与客户端获得了连接 Socket client = serverSocket.accept(); // 处理这次连接 new HandlerThread(client); } } catch (Exception e) { System.out.println("服务器异常: " + e.getMessage()); } } private class HandlerThread implements Runnable { private Socket socket; public HandlerThread(Socket client) { socket = client; new Thread(this).start(); } public void run() { try { // 读取客户端数据 DataInputStream input = new DataInputStream(socket.getInputStream()); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(input)); // 向客户端回复信息 DataOutputStream out = new DataOutputStream(socket.getOutputStream()); while (true) {// String clientInputStr = input.readUTF();//这里要注意和客户端输出流的写方法对应,否则会抛 EOFException String clientInputLine = bufferedReader.readLine(); // 处理客户端数据// System.out.println("客户端发过来的内容:" + clientInputStr);// System.out.println("response from client:" + clientInputStr); System.out.println("response from client:" + clientInputLine);// System.out.print("请输入:\t");// System.out.print("input:\t");// // 发送键盘输入的一行// String s = new BufferedReader(new InputStreamReader(System.in)).readLine();// out.writeUTF(s); }// out.close();// input.close(); } catch (Exception e) { System.out.println("服务器 run 异常: " + e.getMessage()); } finally { if (socket != null) { try { socket.close(); } catch (Exception e) { socket = null; System.out.println("服务端 finally 异常:" + e.getMessage()); } } } } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
客户端:
public class Client { public static final String IP_ADDR = "localhost";//服务器地址 public static final int PORT = 12345;//服务器端口号 public static void main(String[] args) { System.out.println("start client...");// System.out.println("当接收到服务器端字符为 \"OK\" 的时候, 客户端将终止\n"); System.out.println("stop client when received char is \"OK\" \n"); Socket socket = null; try { //创建一个流套接字并将其连接到指定主机上的指定端口号 socket = new Socket(IP_ADDR, PORT); //读取服务器端数据 DataInputStream input = new DataInputStream(socket.getInputStream()); //向服务器端发送数据 DataOutputStream out = new DataOutputStream(socket.getOutputStream()); while (true) { out.write("12123\n".getBytes());// System.out.print("请输入: \t"); System.out.print("input: \t"); String str = new BufferedReader(new InputStreamReader(System.in)).readLine();// out.writeUTF(str); out.write(str.getBytes()); String ret = input.readUTF();// System.out.println("服务器端返回过来的是: " + ret); System.out.println("response from server : " + ret); // 如接收到 "OK" 则断开连接 if ("OK".equals(ret)) {// System.out.println("客户端将关闭连接"); System.out.println("client will close"); Thread.sleep(500); break; } } out.close(); input.close(); } catch (Exception e) { System.out.println("客户端异常:" + e.getMessage()); } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { socket = null; System.out.println("客户端 finally 异常:" + e.getMessage()); } } } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
read()
如果服务器已经断开socket连接,此时再客户端使用boolean b1 = mSocket.isConnected();
还是会返回true,但是使用read()会一直返回-1
之所以会出现连接断开了,但是isConnected()还是会返回true,我认为是这样的,因为tcp连接只是一种状态,服务端和客户端保持这个状态,表示有数据到来,各自会接收,也可以向对方发送数据。其实世界上所有的可以上网的设备都已经存在了物理连接,只不过需要通过各自设备状态(比如:tcp的各个状态),来允许来和谁通信。
readLine()
BufferedReader的readLine方法是一次读一行的,这个方法是阻塞的,
直到它读到了一行数据为止程序才会继续往下执行
直到程序遇到了换行符或者是对应流的结束符readLine方法才会认为读到了一行,
才会结束其阻塞,让程序继续往下执行
所以在写入的时候字符串结尾用换行符就行了 bw.write(“你好服务器\n”);
readUTF()
readUTF读取的必须是writeUTF写下的字符串。
参考:
在Java中readUTF()怎莫用?
android socket readline()方法读不到值的问题
一个 Java 的 Socket 服务器和客户端通信的例子
关注我的公众号,轻松了解和学习更多技术
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow