Java核心代码例程之:ClientSocketDemo.java
2008-01-05 08:40:05 来源:WEB开发网核心提示:import java.io.*;import java.net.*;/** * Demonstrates how to write a Java client * * @see ServerSocketDemo ***/public class ClientSocketDemo{public static void
import java.io.*;
import java.net.*;
/**
* Demonstrates how to write a Java client
*
* @see ServerSocketDemo
***/
public class ClientSocketDemo
{
public static void main(String args[]) throws Exception
{
Socket s = new Socket("localhost", 6000);
PRintWriter pw = new PrintWriter(s.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println(">> Connection with server established");
pw.println("Hello World!");
pw.flush();
// Print input
System.out.println(">> Reading output from server");
System.out.print(br.readLine());
System.out.println("
>> Done!");
// close sockets
pw.close();
br.close();
s.close();
}
}
更多精彩
赞助商链接