import java.net.*;
import java.io.*;
public class Server_002 {
public Server_002() {
try{
ServerSocket SS = new ServerSocket(1234);
System.out.println("Server is created and waiting Client to connect...");
Socket socket = SS.accept();
System.out.println("connected from Client: " + socket.getInetAddress());
System.out.println("Server Local Port: " + socket.getLocalPort());
System.out.println("Client Port: " + socket.getPort());
socket.close();
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
public static void main(String args[]){
Server_002 ServerStart=new Server_002();
}
}
connected from Client: /---.---.---.--- IP
Server Local Port: 1234
Client Port: 59456
----------------------------------------------------------------------------
import java.io.*;
import java.net.*;
java.net.Socket
產生的新物件,可依網站之IP和port,與Server網站連接,
進而以網路串流執行遠端讀取或寫入。
public class Client_002 {
public Client_002() {
try{
InetAddress ina = InetAddress.getByName("IP");
Socket socket = new Socket(ina, 1234);
System.out.println("Client Local Port: "+ socket.getLocalPort());
System.out.println("Server Port: "+ socket.getPort());
socket.close();
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
public static void main(String args[]) {
Client_002 ClientStart = new Client_002();
}
}
Client Local Port: 59456***
Server Port: 1234*****
沒有留言:
張貼留言