WEB开发网
开发学院WEB开发Jsp Java核心代码例程之:ClientSocketDemo.java 阅读

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();
  }
}

Tags:Java 核心 代码

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接