Java Socket編程實(shí)例(一)- TCP基本使用
一.服務(wù)端代碼:
import java.net.*; // for Socket, ServerSocket, and InetAddress import java.io.*; // for IOException and Input/OutputStream public class TCPEchoServer { private static final int BUFSIZE = 32; // Size of receive buffer public static void main(String[] args) throws IOException { int servPort = 5500; // Create a server socket to accept client connection requests ServerSocket servSock = new ServerSocket(servPort); int recvMsgSize; // Size of received message byte[] receiveBuf = new byte[BUFSIZE]; // Receive buffer while (true) { // Run forever, accepting and servicing connections Socket clntSock = servSock.accept(); // Get client connection SocketAddress clientAddress = clntSock.getRemoteSocketAddress(); System.out.println("Handling client at " + clientAddress); InputStream in = clntSock.getInputStream(); OutputStream out = clntSock.getOutputStream(); // Receive until client closes connection, indicated by -1 return while ((recvMsgSize = in.read(receiveBuf)) != -1) { out.write(receiveBuf, 0, recvMsgSize); } clntSock.close(); // Close the socket. We are done with this client! } /* NOT REACHED */ } }
二.客戶端代碼:
import java.net.*; import java.io.*; public class TCPEchoClient { public static void main(String[] args) throws IOException { String server = "127.0.0.1"; // Server name or IP address int servPort = 5500; //// Server port byte[] data = "Hi, World".getBytes(); // Create socket that is connected to server on specified port Socket socket = new Socket(server, servPort); System.out.println("Connected to server...sending echo string"); InputStream in = socket.getInputStream(); OutputStream out = socket.getOutputStream(); out.write(data); // Send the encoded string to the server // Receive the same string back from the server int totalBytesRcvd = 0; // Total bytes received so far int bytesRcvd; // Bytes received in last read while (totalBytesRcvd < data.length) { if ((bytesRcvd = in.read(data, totalBytesRcvd, data.length - totalBytesRcvd)) == -1) throw new SocketException("Connection closed prematurely"); totalBytesRcvd += bytesRcvd; } // data array is full System.out.println("Received: " + new String(data)); socket.close(); // Close the socket and its streams } }
上述代碼的TCP服務(wù)端是單線程,一次只能服務(wù)一個(gè)客戶端。
查看更多Java的語法,大家可以關(guān)注:《Thinking in Java 中文手冊》、《JDK 1.7 參考手冊官方英文版》、《JDK 1.6 API java 中文參考手冊》、《JDK 1.5 API java 中文參考手冊》,也希望大家多多支持腳本之家。
- Java多線程實(shí)現(xiàn)TCP網(wǎng)絡(luò)Socket編程(C/S通信)
- java實(shí)現(xiàn)基于TCP協(xié)議網(wǎng)絡(luò)socket編程(C/S通信)
- java實(shí)現(xiàn)基于Tcp的socket聊天程序
- Java編程實(shí)現(xiàn)基于TCP協(xié)議的Socket聊天室示例
- Java通過 Socket 實(shí)現(xiàn) TCP服務(wù)端
- JAVA實(shí)現(xiàn)基于Tcp協(xié)議的簡單Socket通信實(shí)例
- Java Socket編程實(shí)例(四)- NIO TCP實(shí)踐
- java實(shí)現(xiàn)一個(gè)簡單TCPSocket聊天室功能分享
- Java實(shí)現(xiàn)Socket的TCP傳輸實(shí)例
- Java 基于TCP Socket 實(shí)現(xiàn)文件上傳
相關(guān)文章
SpringBoot實(shí)現(xiàn)數(shù)據(jù)預(yù)熱的方式小結(jié)
這里用到的數(shù)據(jù)預(yù)熱,就是在項(xiàng)目啟動(dòng)時(shí)將一些數(shù)據(jù)量較大的數(shù)據(jù)加載到緩存中(筆者這里用的Redis),那么在項(xiàng)目啟動(dòng)有哪些方式可以實(shí)現(xiàn)數(shù)據(jù)預(yù)熱呢,本文就來給大家講講幾種實(shí)現(xiàn)數(shù)據(jù)預(yù)熱的方式,需要的朋友可以參考下2023-09-09RestTemplate實(shí)現(xiàn)多種底層HTTP客戶端類庫的切換用法
這篇文章主要為大家詳細(xì)的講解了RestTemplate實(shí)現(xiàn)多種底層HTTP客戶端類庫的切換示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助祝大家多多進(jìn)步2022-03-03MyBatisPlus PaginationInterceptor分頁插件的使用詳解
這篇文章主要介紹了MyBatisPlus PaginationInterceptor分頁插件的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03spring?項(xiàng)目實(shí)現(xiàn)限流方法示例
這篇文章主要為大家介紹了spring項(xiàng)目實(shí)現(xiàn)限流的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07SpringBoot數(shù)據(jù)校驗(yàn)功能的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot數(shù)據(jù)校驗(yàn)功能的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02SpringBoot2.0整合WebSocket代碼實(shí)例
這篇文章主要介紹了SpringBoot2.0整合WebSocket代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11MyBatis-Plus與Druid結(jié)合Dynamic-datasource實(shí)現(xiàn)多數(shù)據(jù)源操作數(shù)據(jù)庫的示例
Dynamic-DataSource 可以和絕大多是連接層插件搭配使用,比如:mybatis,mybatis-plus,hibernate等,本文就來介紹一下MyBatis-Plus與Druid結(jié)合Dynamic-datasource實(shí)現(xiàn)多數(shù)據(jù)源操作數(shù)據(jù)庫的示例,感興趣的可以了解一下2023-10-10Java中數(shù)組的創(chuàng)建與傳參方法(學(xué)習(xí)小結(jié))
這篇文章主要介紹了Java中數(shù)組的創(chuàng)建與傳參方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09MAC算法之消息摘要算法HmacMD5的實(shí)現(xiàn)
這篇文章主要介紹了MAC算法之消息摘要算法HmacMD5的實(shí)現(xiàn)的相關(guān)資料,這里提供實(shí)例,幫助大家學(xué)習(xí)理解這部分知識,需要的朋友可以參考下2017-08-08