利用多线程与网络编程编写的实时聊天小程序
生活随笔
收集整理的這篇文章主要介紹了
利用多线程与网络编程编写的实时聊天小程序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
客戶端代碼:
package?Oracle;
import?java.io.BufferedReader;
import?java.io.IOException;
import?java.io.InputStreamReader;
import?java.io.PrintStream;
import?java.net.Socket;
public?class?ClientThread?{
static?Socket?socket?=?null;
public?static?void?main(String[]?args)?throws?Exception?{
socket?=?new?Socket("192.168.3.59",?10002);
System.out.println("客戶端已啟動...");
ClientSent?cs?=?new?ClientSent();
Thread?t1?=?new?Thread(cs);
t1.start();
ClientReceive?cr?=?new?ClientReceive();
Thread?t2?=?new?Thread(cr);
t2.start();
}
}
class?ClientSent?extends?ClientThread?implements?Runnable?{
public?void?run()?{
while?(true)?{
BufferedReader?br?=?new?BufferedReader(new?InputStreamReader(System.in));
PrintStream?ps;
try?{
ps?=?new?PrintStream(socket.getOutputStream(),?true);
String?str?=?null;
str?=?br.readLine();
ps.println(str);
if?("exit".equals(str))?{
System.out.println("已退出");
System.exit(-1);
}
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}
}
class?ClientReceive?extends?ClientThread?implements?Runnable?{
public?void?run()?{
while?(true)?{
BufferedReader?br2;
try?{
br2?=?new?BufferedReader(new?InputStreamReader(socket.getInputStream()));
String?str2?=?null;
str2?=?br2.readLine();
if?("exit".equals(str2))?{
System.out.println("對方退出聊天");
System.exit(-1);
}
System.out.println(socket.getInetAddress().getHostName()?+?"說:"?+?str2);
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}
}
服務端代碼:
package?Oracle;
import?java.io.BufferedReader;
import?java.io.IOException;
import?java.io.InputStreamReader;
import?java.io.PrintStream;
import?java.net.ServerSocket;
import?java.net.Socket;
public?class?ServerThread?{
static?ServerSocket?ss;
static?Socket?s;
public?static?void?main(String[]?args)?throws?IOException?{
ss?=?new?ServerSocket(10002);
System.out.println("服務端已啟動...");
s?=?ss.accept();
String?name?=?s.getInetAddress().getHostName();
System.out.println(name?+?":已連接");
ServerSend?ss?=?new?ServerSend();
Thread?t1?=?new?Thread(ss);
t1.start();
ServerReceive?sr?=?new?ServerReceive();
Thread?t2?=?new?Thread(sr);
t2.start();
}
}
class?ServerSend?extends?ServerThread?implements?Runnable?{
public?void?run()?{
while?(true)?{
BufferedReader?br?=?new?BufferedReader(new?InputStreamReader(System.in));
PrintStream?ps;
try?{
ps?=?new?PrintStream(s.getOutputStream(),?true);
String?str?=?null;
str?=?br.readLine();
ps.println(str);
if?("exit".equals(str))?{
System.out.println("已退出");
System.exit(-1);
}
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}
}
class?ServerReceive?extends?ServerThread?implements?Runnable?{
public?void?run()?{
while?(true)?{
try?{
BufferedReader?br2?=?new?BufferedReader(new?InputStreamReader(s.getInputStream()));
String?str2?=?null;
str2?=?br2.readLine();
System.out.println(s.getInetAddress().getHostName()?+?"說:"?+?str2);
if?("exit".equals(str2))?{
System.out.println("對方退出聊天");
System.exit(-1);
}
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}
}
package?Oracle;
import?java.io.BufferedReader;
import?java.io.IOException;
import?java.io.InputStreamReader;
import?java.io.PrintStream;
import?java.net.Socket;
public?class?ClientThread?{
static?Socket?socket?=?null;
public?static?void?main(String[]?args)?throws?Exception?{
socket?=?new?Socket("192.168.3.59",?10002);
System.out.println("客戶端已啟動...");
ClientSent?cs?=?new?ClientSent();
Thread?t1?=?new?Thread(cs);
t1.start();
ClientReceive?cr?=?new?ClientReceive();
Thread?t2?=?new?Thread(cr);
t2.start();
}
}
class?ClientSent?extends?ClientThread?implements?Runnable?{
public?void?run()?{
while?(true)?{
BufferedReader?br?=?new?BufferedReader(new?InputStreamReader(System.in));
PrintStream?ps;
try?{
ps?=?new?PrintStream(socket.getOutputStream(),?true);
String?str?=?null;
str?=?br.readLine();
ps.println(str);
if?("exit".equals(str))?{
System.out.println("已退出");
System.exit(-1);
}
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}
}
class?ClientReceive?extends?ClientThread?implements?Runnable?{
public?void?run()?{
while?(true)?{
BufferedReader?br2;
try?{
br2?=?new?BufferedReader(new?InputStreamReader(socket.getInputStream()));
String?str2?=?null;
str2?=?br2.readLine();
if?("exit".equals(str2))?{
System.out.println("對方退出聊天");
System.exit(-1);
}
System.out.println(socket.getInetAddress().getHostName()?+?"說:"?+?str2);
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}
}
服務端代碼:
package?Oracle;
import?java.io.BufferedReader;
import?java.io.IOException;
import?java.io.InputStreamReader;
import?java.io.PrintStream;
import?java.net.ServerSocket;
import?java.net.Socket;
public?class?ServerThread?{
static?ServerSocket?ss;
static?Socket?s;
public?static?void?main(String[]?args)?throws?IOException?{
ss?=?new?ServerSocket(10002);
System.out.println("服務端已啟動...");
s?=?ss.accept();
String?name?=?s.getInetAddress().getHostName();
System.out.println(name?+?":已連接");
ServerSend?ss?=?new?ServerSend();
Thread?t1?=?new?Thread(ss);
t1.start();
ServerReceive?sr?=?new?ServerReceive();
Thread?t2?=?new?Thread(sr);
t2.start();
}
}
class?ServerSend?extends?ServerThread?implements?Runnable?{
public?void?run()?{
while?(true)?{
BufferedReader?br?=?new?BufferedReader(new?InputStreamReader(System.in));
PrintStream?ps;
try?{
ps?=?new?PrintStream(s.getOutputStream(),?true);
String?str?=?null;
str?=?br.readLine();
ps.println(str);
if?("exit".equals(str))?{
System.out.println("已退出");
System.exit(-1);
}
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}
}
class?ServerReceive?extends?ServerThread?implements?Runnable?{
public?void?run()?{
while?(true)?{
try?{
BufferedReader?br2?=?new?BufferedReader(new?InputStreamReader(s.getInputStream()));
String?str2?=?null;
str2?=?br2.readLine();
System.out.println(s.getInetAddress().getHostName()?+?"說:"?+?str2);
if?("exit".equals(str2))?{
System.out.println("對方退出聊天");
System.exit(-1);
}
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}
}
總結
以上是生活随笔為你收集整理的利用多线程与网络编程编写的实时聊天小程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 21款奔驰S400商务型升级原厂HUD抬
- 下一篇: PS吸管工具