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 中文手冊(cè)》、《JDK 1.7 參考手冊(cè)官方英文版》、《JDK 1.6 API java 中文參考手冊(cè)》、《JDK 1.5 API java 中文參考手冊(cè)》,也希望大家多多支持腳本之家。
- 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é)議的簡(jiǎn)單Socket通信實(shí)例
- Java Socket編程實(shí)例(四)- NIO TCP實(shí)踐
- java實(shí)現(xiàn)一個(gè)簡(jiǎn)單TCPSocket聊天室功能分享
- Java實(shí)現(xiàn)Socket的TCP傳輸實(shí)例
- Java 基于TCP Socket 實(shí)現(xiàn)文件上傳
相關(guān)文章
SpringCloud微服務(wù)剔除下線功能實(shí)現(xiàn)原理分析
SpringCloud是一種微服務(wù)的框架,利用它我們可以去做分布式服務(wù)開發(fā),這篇文章主要介紹了SpringCloud微服務(wù)剔除下線功能,需要的朋友可以參考下2022-11-11
java 中如何獲取字節(jié)碼文件的相關(guān)內(nèi)容
這篇文章主要介紹了java 中如何獲取字節(jié)碼文件的相關(guān)內(nèi)容的相關(guān)資料,需要的朋友可以參考下2017-04-04
Spring Boot 2.0.0 終于正式發(fā)布-重大修訂版本
北京時(shí)間 2018 年 3 月 1 日早上,如約發(fā)布的 Spring Boot 2.0 在同步至 Maven 倉庫時(shí)出現(xiàn)問題,導(dǎo)致在 GitHub 上發(fā)布的 v2.0.0.RELEASE 被撤回2018-03-03
spring mvc實(shí)現(xiàn)登錄賬號(hào)單瀏覽器登錄
這篇文章主要為大家詳細(xì)介紹了spring mvc實(shí)現(xiàn)登錄賬號(hào)單瀏覽器登錄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
springboot整合mongodb并實(shí)現(xiàn)crud步驟詳解
這篇文章主要介紹了springboot整合mongodb并實(shí)現(xiàn)crud,本文分步驟通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08

