Java實(shí)現(xiàn)簡(jiǎn)單的socket通信教程
今天學(xué)習(xí)了一下java如何實(shí)現(xiàn)socket通信,感覺(jué)難點(diǎn)反而是在io上,因?yàn)閖ava對(duì)socket封裝已經(jīng)很完善了。
今天代碼花了整個(gè)晚上調(diào)試,主要原因是io的flush問(wèn)題和命令行下如何運(yùn)行具有package的類,不過(guò)最后問(wèn)題基本都解決了,把代碼貼出來(lái)供大家參考
server
public class TcpServer { public static void main(String[] args) throws Exception { ServerSocket server = new ServerSocket(9091); try { Socket client = server.accept(); try { BufferedReader input = new BufferedReader(new InputStreamReader(client.getInputStream())); boolean flag = true; int count = 1; while (flag) { System.out.println(客戶端要開(kāi)始發(fā)騷了,這是第 + count + 次!); count++; String line = input.readLine(); System.out.println(客戶端說(shuō): + line); if (line.equals(exit)) { flag = false; System.out.println(客戶端不想玩了!); } else { System.out.println(客戶端說(shuō): + line); } } } finally { client.close(); } } finally { server.close(); } } }
client
public class TcpClient { public static void main(String[] args) throws Exception { Socket client = new Socket(127.0.0.1, 9091); try { PrintWriter output = new PrintWriter(client.getOutputStream(), true); Scanner cin = new Scanner(System.in); String words; while (cin.hasNext()) { words = cin.nextLine(); output.println(words); System.out.println(寫(xiě)出了數(shù)據(jù): + words); } cin.close(); } finally { client.close(); } } }
補(bǔ)充知識(shí):Java-Socket通信 同時(shí)JSON傳遞與解析
服務(wù)端
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; import net.sf.json.JSONObject; public class Server_2 { public static void main(String[] args) { Scanner in=new Scanner(System.in); InputStreamReader isr; BufferedReader br; OutputStreamWriter osw; BufferedWriter rw; try { ServerSocket serverSocket=new ServerSocket(4444); Socket socket=serverSocket.accept(); isr=new InputStreamReader(socket.getInputStream()); br=new BufferedReader(isr); String str=br.readLine(); JSONObject object=JSONObject.fromObject(str); System.out.println("ID:"+object.getInt("ID")); System.out.println("Name:"+object.getString("name")); System.out.println("password:"+object.getString("password")); br.close(); socket.close(); serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } }
客服端
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; import java.util.Scanner; import net.sf.json.JSONObject; public class Client_2 { public static void main(String[] args) { Scanner in = new Scanner(System.in); InputStreamReader isr; BufferedReader br; OutputStreamWriter osw; BufferedWriter rw; try { Socket socket = new Socket("localhost", 4444); osw = new OutputStreamWriter(socket.getOutputStream()); rw = new BufferedWriter(osw); User user = new User(); System.out.println("Id:"); user.setID(in.nextInt()); in.nextLine(); System.out.println("Name:"); user.setName(in.nextLine()); System.out.println("Password:"); user.setPassword(in.nextLine()); JSONObject jsonObject = JSONObject.fromObject(user); rw.write(jsonObject.toString()+"\n"); rw.close(); socket.close(); } catch (Exception e) { // TODO: handle exception } } }
User
public class User { int ID; String name; String password; public int getID() { return ID; } public void setID(int iD) { ID = iD; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
以上這篇Java實(shí)現(xiàn)簡(jiǎn)單的socket通信教程就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
教你在 IntelliJ IDEA 中使用 VIM插件的詳細(xì)教程
這篇文章主要介紹了在 IntelliJ IDEA 中使用 VIM的教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05mybatis 使用jdbc.properties文件設(shè)置不起作用的解決方法
這篇文章主要介紹了mybatis 使用jdbc.properties文件設(shè)置不起作用的解決方法,需要的朋友可以參考下2018-03-03Mybatis實(shí)現(xiàn)分頁(yè)的注意點(diǎn)
Mybatis提供了強(qiáng)大的分頁(yè)攔截實(shí)現(xiàn),可以完美的實(shí)現(xiàn)分功能。下面小編給大家分享小編在使用攔截器給mybatis進(jìn)行分頁(yè)所遇到的問(wèn)題及注意點(diǎn),需要的朋友一起看看吧2017-07-07Spring Cloud 配置中心多環(huán)境配置bootstrap.yml的實(shí)現(xiàn)方法
spring cloud用上了配置中心,就一個(gè)boostrap.yml,本文就來(lái)介紹一下Spring Cloud 配置中心多環(huán)境配置bootstrap.yml的實(shí)現(xiàn)方法,感興趣的可以了解一下2024-03-03springboot結(jié)合前端實(shí)現(xiàn)網(wǎng)頁(yè)跳轉(zhuǎn)功能實(shí)例
今天處理Springboot統(tǒng)一異常攔截的時(shí)候,遇到了頁(yè)面跳轉(zhuǎn)的問(wèn)題,這篇文章主要給大家介紹了關(guān)于springboot結(jié)合前端實(shí)現(xiàn)網(wǎng)頁(yè)跳轉(zhuǎn)功能的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12redis分布式鎖RedissonLock的實(shí)現(xiàn)細(xì)節(jié)解析
這篇文章主要介紹了redis分布式鎖RedissonLock的實(shí)現(xiàn)細(xì)節(jié)解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06淺談Storm在zookeeper上的目錄結(jié)構(gòu)
這篇文章主要介紹了淺談Storm在zookeeper上的目錄結(jié)構(gòu)的相關(guān)內(nèi)容,涉及storm使用zookeeper的操作以及詳細(xì)結(jié)構(gòu)圖,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10