通过多线程和IO流来实现服务端和客户端互动对话

本文详细介绍了一种基于Java的Socket通信实现方式,包括服务端和客户端的建立、信息的发送与接收。通过多线程处理,实现了双向数据传输。

服务端

package com.softeem.TTP;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Currency;
import java.util.Scanner;

import javax.swing.JOptionPane;

public class Test4_Server {


public static void main(String[] args) throws IOException {
	// 创建一个服务端套接字插座
	ServerSocket ss = new ServerSocket(8888);
	System.out.println("等待客户连接。。。。");
	Socket s = ss.accept();
	System.out.println("连接成功");
	//实例化一个Message1对象 
	Message1 sm = new Message1(s);
	//启动线程
	new Thread(sm, "发送").start();
	new Thread(sm, "接收").start();

}

}

class Message1 implements Runnable {
	//属性部分
	private  Socket s;
	//构造方法
	public Message1(Socket s) {
		this.s = s;


}

public void run() {
	String name = Thread.currentThread().getName();
	//判断当前线程名并采取相应的操作
	if(Thread.currentThread().getName().equals("发送")){
		try {
			write();//调用发送信息的方法
		} catch (IOException e) {
			e.printStackTrace();
		}
	}else{
		try {
			read();//调用接受信息的放法
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
//接受信息的方法
public void  read() throws IOException{
	while(true){
	//创建数据输入流来包装需要从客户端收到的inputStream对象
	DataInputStream   dis = new DataInputStream(s.getInputStream());
	System.out.println("从客户端读取了"+dis.readUTF());
	}
	
}
//发送信息的方法
public void  write() throws IOException{
	while(true){
		//创建数据输出流来包装需要发送的outputStream对象来发送到客户端
		DataOutputStream dos = new DataOutputStream(s.getOutputStream());
		String str = JOptionPane.showInputDialog("请输入要发送的信息");
		 System.out.println("服务器:"+str);
		 dos.writeUTF(str);
	}
}


}

客户端

package com.softeem.TTP;import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.WriteAbortedException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;import javax.swing.JOptionPane;public class Test4_Client {public static void main(String[] args) throws IOException {
    //创建一个插头
    Socket  s = new Socket("localhost",8888);
    //实例化一个Message2对象
    Message2  am = new  Message2(s);
    //启动线程
    new Thread(am,"接受").start();
    new Thread(am,"发送").start();}
}
class Message2 implements Runnable {
	//属性部分
    private  Socket s ;
    //构造方法
	public Message2(Socket s ) {
		this.s= s;
		}    public void run() {
        //判断当前线程名并采取相应的操作
    if(Thread.currentThread().getName().equals("发送")){
        try {
            write();//调用发送信息的方法
        } catch (IOException e) {
            e.printStackTrace();
        }
    }else{
        try {
            read();//调用接受信息的放法
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    }
    //接收信息的方法
    public void read() throws IOException{
        while(true){
            //创建数据输入流来包装需要从服务器收到的inputStream对象
            DataInputStream dis = new DataInputStream(s.getInputStream());
            System.out.println("服务器"+dis.readUTF());
            
        }
    }
    //发送信息的方法
    public void write() throws IOException {
        while(true){
            //创建数据输出流来包装需要发送的outputStream对象来发送到服务器
        DataOutputStream dos = new DataOutputStream(s.getOutputStream());
        String str = JOptionPane.showInputDialog("请输入发送的信息");
        System.out.println("客户端:"+str);
        dos.writeUTF(str);
        }
    }
    }

	
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值