當前位置:編程學習大全網 - 編程語言 - 速求用JAVA語言寫聊天室的源代碼

速求用JAVA語言寫聊天室的源代碼

ClientSocketDemo.java 客戶端Java源代碼

import java.net.*;

import java.io.*;

public class ClientSocketDemo

{

//聲明客戶端Socket對象socket

Socket socket = null;

//聲明客戶器端數據輸入輸出流

DataInputStream in;

DataOutputStream out;

//聲明字符串數組對象response,用於存儲從服務器接收到的信息

String response[];

//執行過程中,沒有參數時的構造方法,本地服務器在本地,取默認端口10745

public ClientSocketDemo()

{

try

{

//創建客戶端socket,服務器地址取本地,端口號為10745

socket = new Socket("localhost",10745);

//創建客戶端數據輸入輸出流,用於對服務器端發送或接收數據

in = new DataInputStream(socket.getInputStream());

out = new DataOutputStream(socket.getOutputStream());

//獲取客戶端地址及端口號

String ip = String.valueOf(socket.getLocalAddress());

String port = String.valueOf(socket.getLocalPort());

//向服務器發送數據

out.writeUTF("Hello Server.This connection is from client.");

out.writeUTF(ip);

out.writeUTF(port);

//從服務器接收數據

response = new String[3];

for (int i = 0; i < response.length; i++)

{

response[i] = in.readUTF();

System.out.println(response[i]);

}

}

catch(UnknownHostException e){e.printStackTrace();}

catch(IOException e){e.printStackTrace();}

}

//執行過程中,有壹個參數時的構造方法,參數指定服務器地址,取默認端口10745

public ClientSocketDemo(String hostname)

{

try

{

//創建客戶端socket,hostname參數指定服務器地址,端口號為10745

socket = new Socket(hostname,10745);

in = new DataInputStream(socket.getInputStream());

out = new DataOutputStream(socket.getOutputStream());

String ip = String.valueOf(socket.getLocalAddress());

String port = String.valueOf(socket.getLocalPort());

out.writeUTF("Hello Server.This connection is from client.");

out.writeUTF(ip);

out.writeUTF(port);

response = new String[3];

for (int i = 0; i < response.length; i++)

{

response[i] = in.readUTF();

System.out.println(response[i]);

}

}

catch(UnknownHostException e){e.printStackTrace();}

catch(IOException e){e.printStackTrace();}

}

//執行過程中,有兩個個參數時的構造方法,第壹個參數hostname指定服務器地址

//第壹個參數serverPort指定服務器端口號

public ClientSocketDemo(String hostname,String serverPort)

{

try

{

socket = new Socket(hostname,Integer.parseInt(serverPort));

in = new DataInputStream(socket.getInputStream());

out = new DataOutputStream(socket.getOutputStream());

String ip = String.valueOf(socket.getLocalAddress());

String port = String.valueOf(socket.getLocalPort());

out.writeUTF("Hello Server.This connection is from client.");

out.writeUTF(ip);

out.writeUTF(port);

response = new String[3];

for (int i = 0; i < response.length; i++)

{

response[i] = in.readUTF();

System.out.println(response[i]);

}

}

catch(UnknownHostException e){e.printStackTrace();}

catch(IOException e){e.printStackTrace();}

}

public static void main(String[] args)

{

String comd[] = args;

if(comd.length == 0)

{

System.out.println("Use localhost(127.0.0.1) and default port");

ClientSocketDemo demo = new ClientSocketDemo();

}

else if(comd.length == 1)

{

System.out.println("Use default port");

ClientSocketDemo demo = new ClientSocketDemo(args[0]);

}

else if(comd.length == 2)

{

System.out.println("Hostname and port are named by user");

ClientSocketDemo demo = new ClientSocketDemo(args[0],args[1]);

}

else System.out.println("ERROR");

}

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

ServerSocketDemo.java 服務器端Java源代碼

import java.net.*;

import java.io.*;

public class ServerSocketDemo

{

//聲明ServerSocket類對象

ServerSocket serverSocket;

//聲明並初始化服務器端監聽端口號常量

public static final int PORT = 10745;

//聲明服務器端數據輸入輸出流

DataInputStream in;

DataOutputStream out;

//聲明InetAddress類對象ip,用於獲取服務器地址及端口號等信息

InetAddress ip = null;

//聲明字符串數組對象request,用於存儲從客戶端發送來的信息

String request[];

public ServerSocketDemo()

{

request = new String[3]; //初始化字符串數組

try

{

//獲取本地服務器地址信息

ip = InetAddress.getLocalHost();

//以PORT為服務端口號,創建serverSocket對象以監聽該端口上的連接

serverSocket = new ServerSocket(PORT);

//創建Socket類的對象socket,用於保存連接到服務器的客戶端socket對象

Socket socket = serverSocket.accept();

System.out.println("This is server:"+String.valueOf(ip)+PORT);

//創建服務器端數據輸入輸出流,用於對客戶端接收或發送數據

in = new DataInputStream(socket.getInputStream());

out = new DataOutputStream(socket.getOutputStream());

//接收客戶端發送來的數據信息,並顯示

request[0] = in.readUTF();

request[1] = in.readUTF();

request[2] = in.readUTF();

System.out.println("Received messages form client is:");

System.out.println(request[0]);

System.out.println(request[1]);

System.out.println(request[2]);

//向客戶端發送數據

out.writeUTF("Hello client!");

out.writeUTF("Your ip is:"+request[1]);

out.writeUTF("Your port is:"+request[2]);

}

catch(IOException e){e.printStackTrace();}

}

public static void main(String[] args)

{

ServerSocketDemo demo = new ServerSocketDemo();

}

}

  • 上一篇:誰知道武進西太湖是什麽鬼地方?
  • 下一篇:如何做壹個投票鏈接
  • copyright 2024編程學習大全網