客戶端:
多個客戶可以正常收發(fā)信息,因為可以同時發(fā)送和接受信息,不是發(fā)送完信息后等待
返回信息,所以要加入多線程
站在用戶的角度思考問題,與客戶深入溝通,找到息縣網(wǎng)站設(shè)計與息縣網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都網(wǎng)站設(shè)計、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、申請域名、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務(wù)覆蓋息縣地區(qū)。
public class Client {
public static void main(String[]args) throws UnknownHostException, IOException
{
System.out.println("客戶端啟動中...");
Socket client =new Socket("localhost",9999);
new Thread(new Send(client)).start();
new Thread(new Receive(client)).start();
}
}
客戶端的發(fā)送端封裝了:
發(fā)送消息
從鍵盤讀取消息
run()
釋放資源
public class Send implements Runnable{
private BufferedReader br;
private DataOutputStream dos;
private Socket client;
private boolean flag=true;
public Send(Socket client)
{
this.client=client;
br=new BufferedReader(new InputStreamReader(System.in));
try {
dos=new DataOutputStream(client.getOutputStream());
} catch (IOException e) {
this.release();
}
}
public void run()
{
while(flag)
{
System.out.println("請輸入消息:");
String msg=getstr();
send(msg);
}
}
private void release()
{
this.flag=false;
utils.close(dos,client);
}
//從控制臺獲取消息
private String getstr()
{
try {
String msg=br.readLine();
return msg;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private void send(String msg)
{
try {
dos.writeUTF(msg);
dos.flush();
} catch (IOException e) {
release();
}
}
}
客戶端的接收端封裝了:
接收消息
run()
釋放資源
public class Receive implements Runnable {
private DataInputStream dis;
private Socket client;
private boolean flag=true;
public Receive(Socket client)
{
this.client=client;
try {
dis=new DataInputStream(client.getInputStream());
} catch (IOException e) {
release();
}
}
private String receive()
{
String msg;
try {
msg = dis.readUTF();
return msg;
} catch (IOException e) {
release();
}
return null;
}
public void run()
{
while(flag)
{
String msg=receive();
System.out.println(msg);
}
}
private void release()
{
this.flag=false;
utils.close(dis,client);
}
}
作為釋放資源工具類
public class utils {
public static void close(Closeable... target )
{
for(Closeable it:target)
{
try {
if(null!=it)
{
it.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
在線聊天室
服務(wù)器:
public class Chat {
public static void main(String[]args) throws IOException
{
System.out.println("服務(wù)器啟動中...");
ServerSocket server=new ServerSocket(9999);
while(true)
{
Socket client =server.accept();
System.out.println("一個客戶端建立了連接");
new Thread(new channel(client)).start();
}
}
static class channel implements Runnable{
private DataInputStream dis;
private DataOutputStream dos;
private BufferedReader br;
private Socket client;
private boolean flag;
public channel(Socket client)
{
this.client=client;
try {
dis=new DataInputStream(client.getInputStream());
dos=new DataOutputStream(client.getOutputStream());
flag=true;
} catch (IOException e) {
release(); }
}
//接收消息
private String receive()
{
try {
String msg=dis.readUTF();
return msg;
} catch (IOException e) {
release();
}
return null;
}
//發(fā)送給消息
private void send(String msg)
{
try {
dos.writeUTF(msg);
dos.flush();
} catch (IOException e) {
release();
}
}
private void release()
{
this.flag=false;
utils.close(dis,dos,client); //寫一個工具類,使用Closeable...可變參數(shù)
}
public void run()
{
while(flag)
{
String msg=receive();
send(msg);
}
release();
}
}
}