㈠ java中伺服器端ServerSocket對象怎麼獲取伺服器端地址和埠號,怎麼獲取遠程請求的
ServerSocket s = new ServerSocket(8888);
while (true) {
// 建立連接
Socket socket = s.accept();
/ /getInetAddress()獲取遠程ip地址,getPort()遠程客戶端的斷後好
"你好,客戶端地專址信息: " + socket.getInetAddress() + "\t客戶端通信埠號屬: " + socket.getPort()
㈡ java 用ServerSocket監聽了一個埠,在程序結束的時候沒有close,現在再次執行程
用 cmd.exe 程序來輸入命令:
netstat-ano|find":你的埠號"
找到了的話:
taskkill/f/pid:你的進程PID(在上版一條命令行中會列出進程PID)
正常的情況下權,如果你的程序真的已經退出了,那個埠就會自動被操作系統釋放的,很可能你的程序還沒有退出,只是隱藏了。
舉例,假如 tomcat 啟動了之後:
netstat-ano|find":8080"
得到 tomcat 進程號PID是 6435,再來:
taskkill/f/pid:6435
㈢ java socket通訊第二次連接失敗
好象兩端的
埠號不一樣啊。
㈣ Java socket 如何 綁定指定的ip和埠號
Sockets=newSocket(ip,port);
然後就可以用s來和伺服器進行通信了
㈤ java 用ServerSocket監聽了一個埠,在程序結束的
用 cmdexe 程序來輸入命令: netstat -ano | find ":你的埠號"找到了的話: taskkill /f /pid:你的進程PID(在上一條命令行中會列出進程PID) 正常的情況下,如果你的程序真的已經退出了,那個埠就會自動被操作系統釋放的,很可能你的程序還java 用ServerSocket監聽了一個埠,在程序結束的
㈥ 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();
}
}
㈦ JAVA中socket的TCP連接個數
如果你能控製程序的話,就用樓上的辦法了。。
否則,用命令阿。。。
比如:
netstat -an 就能得到版全部連接情權況。。
如果是unix,那麼:
netstat -an | grep 80 | wc -l
得到綁定80埠的連接數。