Friday, February 9, 2007

Java Connect to server socket

//โปรแกรมสำหรับตรวจสอบการเปิดพอร์ตจากเว็บอื่น


import java.io.*;
import java.net.*;

public class NetService {

public static void main(String[] args) throws Exception {
Socket s = new Socket("202.44.204.200", 80);
PrintStream ps = new PrintStream(s.getOutputStream());
//String msg = "GET /netservices/checkportall.php?host=www.msu.ac.th HTTP/1.1";
String msg = "GET /netservices/checkportall.php?host=www.yahoo.com";
// ลองตัด GET หรือ HTTP/1.1 รอดูผลการรัน
System.out.println("Connect 202.44.204.200 port 80...");
System.out.println("Send : " + msg);
ps.println(msg);

BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println("Wait...");
while(true) {
String line = br.readLine();
if (line == null) break;
System.out.println(line);
}
System.out.println("End program...");
}

/** Creates a new instance of NetService */
public NetService() {
}

}

No comments:

Post a Comment

Popular Posts