java實現(xiàn)簡單TCP聊天程序

本文實例為大家分享了java實現(xiàn)TCP聊天程序的具體代碼,供大家參考,具體內(nèi)容如下

固安ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!

服務(wù)端代碼:

package com.test.server;
 
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
 
public class Server {
 
 public static void main(String[] args) {
 new Server().startServer();
 }
 
 public void startServer(){
 try {
  //服務(wù)器在9990端口監(jiān)聽客戶端的連接
  ServerSocket ss = new ServerSocket(9999);
  System.out.println("server is listening...");
  while(true){
  //阻塞的accept方法,當一個客戶端連接上,才會返回Socket對象
  Socket s = ss.accept();
  System.out.println("a client has connected!");
  //開啟線程處理通信
  new CommunicateThread(s).start();
  }
 } catch (IOException e) {
  e.printStackTrace();
 }
 }
 
 public class CommunicateThread extends Thread{
 
 Socket socket;
 DataInputStream dis;
 DataOutputStream dos;
 
 public CommunicateThread(Socket socket){
  this.socket = socket;
  try {
  dis = new DataInputStream(socket.getInputStream());
  dos = new DataOutputStream(socket.getOutputStream());
  } catch (IOException e) {
  e.printStackTrace();
  }
 }
 
 @Override
 public void run() {
  super.run();
  //讀取客戶端發(fā)過來的消息
  String msg = null;
  try {
  while((msg = dis.readUTF()) != null){
   System.out.println("client send msg :" + msg);
   String replyMsg = "server reply : " + msg;
   dos.writeUTF(replyMsg);
   System.out.println("server reply msg :" + replyMsg);
  }
  } catch (IOException e) {
  e.printStackTrace();
  }
 }
 }
 
}

客戶端代碼:

package com.test.client;
 
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
 
public class Client {
 
 public static void main(String[] args) {
 new Client().startClient();
 }
 
 public void startClient(){
 try {
  //連接到服務(wù)器
  Socket socket = new Socket("localhost", 9999);
  DataInputStream dis = new DataInputStream(socket.getInputStream());
  DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
  Scanner scanner = new Scanner(System.in);
  String line = null;
  listenServerReply(dis);
  while((line = scanner.nextLine()) != null){//讀取從鍵盤輸入的一行
  dos.writeUTF(line);//發(fā)給服務(wù)端
  System.out.println("client send msg : " + line);
  }
 } catch (Exception e) {
  e.printStackTrace();
 }
 }
 
 //監(jiān)聽服務(wù)端回復(fù)的消息
 public void listenServerReply(final DataInputStream dis){
 new Thread(){
  @Override
  public void run() {
  super.run();
  String line = null;
  try {
   while((line = dis.readUTF()) != null){
   System.out.println("client receive msg from server: " + line);
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
  }
 }.start();
 }
 
}

先啟動服務(wù)端,再啟動客戶端,在客戶端的控制臺輸入消息并回車,就可以發(fā)送消息給客戶端了,客戶端收到消息后,馬上會回復(fù)一個消息

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

文章題目:java實現(xiàn)簡單TCP聊天程序
文章位置:http://bm7419.com/article34/pcdppe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、網(wǎng)站制作關(guān)鍵詞優(yōu)化、手機網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)