JAVA TCP通信练习
生活随笔
收集整理的這篇文章主要介紹了
JAVA TCP通信练习
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
1、Server端
package com.hhdys.serviceimpl;import java.io.*; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.concurrent.TimeUnit;/*** Created by hhdys on 16/7/22.*/ public class Server {private static List<Socket> clients = new ArrayList<Socket>();public static void main(String[] args) {try {System.out.println("begin==================");ServerSocket serverSocket = new ServerSocket(8088);while (true) {System.out.println("請(qǐng)輸入你要進(jìn)行的操作!");Scanner scanner = new Scanner(System.in);while (scanner.hasNextInt()) {int i = scanner.nextInt();if (i == 1) {read(serverSocket);} else {write();}}scanner.close();}} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}System.out.println("end==============");}private static void read(ServerSocket serverSocket) throws Exception {System.out.println("開始監(jiān)聽===" + clients.size());final Socket socket = serverSocket.accept();clients.add(socket);new Thread(new Runnable() {public void run() {try {InputStream in = socket.getInputStream();Scanner scanner = new Scanner(in);while (true) {while (scanner.hasNextLine()) {System.out.println(scanner.nextLine());}TimeUnit.SECONDS.sleep(1);}} catch (IOException e) {e.printStackTrace();}catch (Exception e){e.printStackTrace();}}}).start();}public static void write() throws Exception {System.out.println("發(fā)送server消息====");Socket socket = clients.get(0);OutputStream out = socket.getOutputStream();BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));writer.write("我是服務(wù)端哈哈哈哈!你好");writer.newLine();writer.flush();// writer.close();// out.close();}}2、Client端
package com.hhdys.serviceimpl;import java.io.*; import java.net.Socket; import java.util.Scanner; import java.util.concurrent.TimeUnit;/*** Created by hhdys on 16/7/22.*/ public class Client {private static Socket socket=null;public static void main(String[] args){System.out.println("begin==========");try {socket=new Socket("127.0.0.1",8088);socket.setKeepAlive(true);System.out.println("client======");OutputStream out = socket.getOutputStream();InputStream in = socket.getInputStream();final BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(out));final Scanner scanner1 = new Scanner(in);Scanner scanner=new Scanner(System.in);while(true){System.out.println("輸入你的操作!");if(scanner.hasNextInt()){int i=scanner.nextInt();if(i==1){new Thread(new Runnable() {public void run() {try {writer.write("我是客戶端哈哈哈哈!你好");writer.newLine();writer.flush();} catch (IOException e) {e.printStackTrace();}}}).start();}else{while (scanner1.hasNextLine()) {System.out.println(scanner1.nextLine());}scanner1.close();}}TimeUnit.SECONDS.sleep(1);}} catch (IOException e) {e.printStackTrace();}catch (Exception e){e.printStackTrace();}System.out.println("end===========");} }轉(zhuǎn)載于:https://my.oschina.net/hhdys412/blog/723869
總結(jié)
以上是生活随笔為你收集整理的JAVA TCP通信练习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 胎梦梦到大象是什么意思
- 下一篇: 梦到手腕出血是什么预兆