首页 > 代码库 > Java学习笔记之基于TCP协议的socket
Java学习笔记之基于TCP协议的socket
可以一直输入,而不是一问一答:
开两个线程,一个负责收,一个负责发。
1.先运行:
package com.zr.javase0825;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;public class User1 implements Runnable { //String pdd="read"; Socket sock; //Socket sockC; public static void main(String[] args) { User1 u1=new User1(); ServerSocket ssock; try { ssock = new ServerSocket(1234); u1.sock=ssock.accept(); // u1.sockC=new Socket("127.0.0.1",5678); Thread t1=new Thread(u1,"read"); Thread t2=new Thread(u1,"write"); t1.start(); t2.start(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void read(Socket sock){ try { BufferedReader buff=new BufferedReader(new InputStreamReader(sock.getInputStream())); System.out.println("user2:"+buff.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void write(Socket sockC){ try { BufferedWriter buff=new BufferedWriter(new OutputStreamWriter(sockC.getOutputStream())); BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); String str= bf.readLine(); buff.write(str+"\n"); buff.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void run() { if(Thread.currentThread().getName().equals("read")) while(true) read(sock); else while(true) write(sock); } }
后运行:
package com.zr.javase0825;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.net.Socket;import java.util.Scanner;public class User2 implements Runnable { //String pdd="read"; Socket sock; //Socket sockC; public static void main(String[] args) { User2 u1=new User2(); //ServerSocket ssock; try { //ssock = new ServerSocket(5678); //u1.sock=ssock.accept(); u1.sock=new Socket("127.0.0.1",1234); Thread t1=new Thread(u1,"read"); Thread t2=new Thread(u1,"write"); t1.start(); t2.start(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void read(Socket sock){ try { BufferedReader buff=new BufferedReader(new InputStreamReader(sock.getInputStream())); System.out.println("user2:"+buff.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void write(Socket sockC){ try { BufferedWriter buff=new BufferedWriter(new OutputStreamWriter(sockC.getOutputStream())); BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); String str= bf.readLine(); buff.write(str+"\n"); buff.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void run() { Scanner in=new Scanner(System.in); if(Thread.currentThread().getName().equals("read")) while(true) read(sock); else while(true) write(sock); } }
Java学习笔记之基于TCP协议的socket
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。