JAVA TCP简易聊天软件

服务器

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class TCPS {
 public static void main(String[] args) throws Exception {
  ServerSocket ss = new ServerSocket(5025);
  Socket sc = ss.accept();
  Sends sends=new Sends(sc);
  sends.start();
  Gets gets=new Gets(sc);
  gets.start();

 }
}

class Sends extends Thread {
 Socket sc;

 public Sends(Socket sc) {
  this.sc = sc;
 }

 public void run() {
  while (true) {
   try {
    sleep(1000);
   } catch (InterruptedException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }
   try {
    OutputStream os = sc.getOutputStream();
    String str1 = new Scanner(System.in).next();
    os.write(str1.getBytes());
//    os.close();  //流不要关,不然端口会马上被关掉就无法继续聊天了
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
}

class Gets extends Thread {
 Socket sc;

 public Gets(Socket sc) {
  this.sc = sc;
 }

 public void run() {
  while (true) {
   try {
    sleep(1000);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   try {
    InputStream is=sc.getInputStream();
    InputStreamReader isr=new InputStreamReader(is);
    int i;
    while ((i=isr.read())!=-1){
     System.out.println((char)i);
    }
    isr.close();
    is.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
}

客户端

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class TCPK {
 public static void main(String[] args) throws UnknownHostException, IOException {
  Socket s=new Socket("192.168.1.102",5025);
  Sendk sendk=new Sendk(s);
  sendk.start();
  Getk getk=new Getk(s);
  getk.start();
  
 }
}
class Sendk extends Thread{
 Socket s;
 public Sendk(Socket s) {
  this.s=s;
 }
 public void run() {
  while (true) {
   try {
    sleep(1000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   try {
    OutputStream os=s.getOutputStream();
    String s1=new Scanner(System.in).next();
    os.write(s1.getBytes());
//    os.close(); //流不要关,不然端口会马上被关掉就无法继续聊天了
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
 
}
class Getk extends Thread{
 Socket s;
 public Getk(Socket s) {
  this.s=s;
 }
 public void run() {
  while (true) {
   try {
    sleep(1000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   try {
    InputStream is=s.getInputStream();
    InputStreamReader isr=new InputStreamReader(is);
    int i;
    while ((i=isr.read())!=-1) {
     System.out.println((char)i);
    }
    isr.close();
    is.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
   
  }
 }
 
}


转载于:https://my.oschina.net/2892328252/blog/644732

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值