Java学习笔记之基于TCP协议的socket

Posted 不知为何就叫呵呵

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

Java网络编程基础-- 基于TCP/IP的Socket编程

Socket网络编程学习笔记 TCP快速入门

JAVA Socket 编程学习笔记

基于TCP协议之——socket编程

Java学习笔记53(网络编程:TCP协议案例)

java socket通信