posts - 37, comments - 8, trackbacks - 0, articles - 0

java基础:关于网络编程

Posted on 2008-08-04 21:36 梦与桥 阅读(339) 评论(0)  编辑  收藏 所属分类: java基础
1、InetAddress类
InetAddress类没有提供构造函数,只可以调用一些静态方法来生成。
2、使用URL方式进行网络连接:

import java.io.*;
import java.net.*;
public class Test
{
 public static void main(String args[])
 {
  try
  {
   URL url=new URL("http://www.baidu.com");
   BufferedReader br=new BufferedReader(
    new InputStreamReader(url.openStream()));
   String str;
   while((str=br.readLine())!=null)
   {
    System.out.println(str);
   }

    br.close();
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
  
 }
}

3、基于TCP的socke编程:
        图解:

code:
服务器端:

import java.io.*;
import java.net.*;
public class xServer
{
 public static final int PORT=4500;
 public static void main(String args[])throws IOException
 {
  ServerSocket s=new ServerSocket(PORT);
  System.out.println("Begin:"+s);
  try
  {
   Socket socket=s.accept();
   try
   {
    System.out.println("Connection accepted:"+socket);
    BufferedReader in=new BufferedReader(
     new InputStreamReader(socket.getInputStream()));
    PrintWriter out=new PrintWriter(new BufferedWriter(
     new OutputStreamWriter(socket.getOutputStream())),true);
    while(true)
    {
     String st=in.readLine();
     if(st.equals("END"))break;
     System.out.println("Recived:"+st);
     out.println("OK:");
    }
   }
   finally
   {
    System.out.println("closing...");
    socket.close();
   }
  }
  finally
  {
   s.close();
  }
 }

}

客户端:
import java.io.*;
import java.net.*;
public class xClient
{
 public static void main(String[] args)throws IOException
 {
  InetAddress addr=InetAddress.getByName("localhost");
  System.out.println("addr="+addr);
  Socket socket =new Socket(addr,xServer.PORT);
  try
  {
   System.out.println("socket="+socket);
   BufferedReader in=new BufferedReader(
     new InputStreamReader(socket.getInputStream()));
   PrintWriter out=new PrintWriter(new BufferedWriter(
     new OutputStreamWriter(socket.getOutputStream())),true);
   for(int i=0;i<5;i++)
   {
    out.println(i+"Hello!");
    String st=in.readLine();
    System.out.println(">>"+st);
   }
   out.println("END");
  }
  finally
  {
   System.out.println("closing...");
   socket.close();
  }
 }
}

4、基于UDP的Socket编程:
服务器端:
import java.io.*;
import java.lang.*;
import java.net.*;
public class UDPServerDemo
{
 private DatagramSocket skServer;
 private DatagramPacket dpServer;
 private byte rb[];
 private String strReceived;
 public UDPServerDemo()
 {
  Init();
 }
 public void Init()
 {
  try
  {
   skServer=new DatagramSocket(10005);
   System.out.println("服务器已启动并已处于监听状态!");
   rb=new byte[1024];
   dpServer=new DatagramPacket(rb,rb.length);
   strReceived="";
   int i=0;
   while(i==0)
   {
    //System.out.println("i="+i);
    skServer.receive(dpServer);
    i=dpServer.getLength();
    if(i>0)
    {
     strReceived=new String(rb,0,dpServer.getLength());
     System.out.println("[from Client]"+strReceived);i=0;
    }
   }
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
 }
 public static void main(String args[])
 {
  new UDPServerDemo();
 }
}

客户端;
import java.io.*;
import java.lang.*;
import java.net.*;
public class UDPClientDemo
{
 private DatagramSocket dsClient;
 private DatagramPacket dpClient;
 private byte sb[];
 private String strSend;
 public UDPClientDemo()
 {
  Init();
 }
 public void Init()
 {
  try
  {
   dsClient=new DatagramSocket(10002);
   sb=new byte[1024];
   strSend="使用UDP方式发送的数据";
   sb=strSend.getBytes();
   dpClient=new DatagramPacket(sb,sb.length,InetAddress.getByName("localhost"),10005);
   System.out.println("开始发送数据...");
   dsClient.send(dpClient);
   System.out.println("数据发送完毕!");
  }
  catch (SocketException se)
  {
   se.printStackTrace();
  }
  catch (IOException ie)
  {
   ie.printStackTrace();
  }
 }
 public static void main(String args[])
 {
  new UDPClientDemo();
 }
}

只有注册用户登录后才能发表评论。


网站导航: