操作简便,支付安全,轻松赚美元!

Java的Socket编程

package aaa;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;



public class Sample extends Thread
{
	private Socket s;
	
	public Sample(Socket s)
	{
		this.s=s;
	}
	
	public void run()
	{
		try {
			OutputStream os = s.getOutputStream();
			InputStream is = s.getInputStream();
			os.write("Hello,welcome you!".getBytes());
			byte[] buf = new byte[100];
			int len=is.read(buf);
			System.out.println(new String(buf, 0, len));
			os.close();
			is.close();
			s.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args)
	{
		if(args.length>0)                //配置一下运行方式,当有参数传入时运行server()方法
			server();    
		else 							 //否则,运行client()方法
			client();
	}
		
	public static void server()
	{
		try {
				ServerSocket ss = new ServerSocket(6000);
				while(true){
					Socket s=ss.accept();
					new Sample(s).start();
					//ss.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		public static void client()
		{
			try {
				Socket s = new Socket(InetAddress.getByName(null), 6000);
				OutputStream os = s.getOutputStream();
				InputStream is = s.getInputStream();
				byte[] buf = new byte[100];
				int len = is.read(buf);
				System.out.println(new String(buf, 0, len));
				os.write("Hello,this is quan!".getBytes());
				os.close();
				is.close();
				s.close();
				
			} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
}

三种方式获取本机IP地址:
 InetAddress.getByName(null)
  InetAddress.getByName("localhost")
  InetAddress.getByName("127.0.0.1")

以上是基于TCP的socket编程。其中
服务器程序编写:
1、调用ServerSocket(int port)创建一个服务器端套接字,并绑定到指定的端口上;
2、调用accept(),监听连接请求,如果客户端请求连接,则接受连接,返回通信套接字;
3、调用Socket类的getOutputStream()和getInputStream()获取输出流和输入流,开始网络数据的发送和接收;
4、最后关闭通信套接字。

客户端程序编写:
1、调用Socket()创建一个流套接字,并连接服务器端;
2、调用Socket类的getOutputStream()和getInputStream()获取输出流和输入流,开始网络数据的发送和接收;
3、最后关闭通信套接字。

package aaa;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;



public class Sample extends Thread
{
	public static void main(String[] args)
	{
		if(args.length>0)
			recive();
		else
			send();
	}
	
	public static void recive()
	{
		try {
			DatagramSocket ds = new DatagramSocket(6000);
			byte[] buf = new byte[100];
			DatagramPacket dp = new DatagramPacket(buf, 100);
			ds.receive(dp);
			System.out.println(new String(buf, 0, dp.getLength()));
			
			String str = "Welcome you!";
			DatagramPacket dpSend = new DatagramPacket(str.getBytes(), str.length(),
					dp.getAddress(), dp.getPort());
			ds.send(dpSend);
			
			ds.close();
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void send()
	{
		try {
			DatagramSocket ds=new DatagramSocket();
			String str = "Hello,this is yihan!";
			DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), 
					InetAddress.getByName("localhost"), 6000);
			ds.send(dp);
			
			byte[] buf = new byte[100];
			DatagramPacket dpRecv = new DatagramPacket(buf, 100);
			ds.receive(dpRecv);
			System.out.println(new String(buf,0,dpRecv.getLength()));
			
			ds.close();
			
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

基于UDP的socket编程
接收端程序编写:
1、调用DatagramSocket(int port)创建一个数据报套接字,并绑定到指定端口上;
2、调用DatagramPacket(byte[] buf,int length),建立一个字节数组以接收UDP包;
3、调用Datagramsocket类的receive(),接收UDP包;
4、最后关闭数据报套接字;

发送端程序编写:
1、调用DatagramSocket()创建一个数据报套接字;
2、调用DatagramPacket(byte[] buf,int offset,int length,InetAddress address,int port),建立要

发送的UDP包;
3、调用DatagramSocket类的send(),发送UDP包;
4、最后关闭数据报套接字。

posted @ 2011-07-16 16:15  逍宇  阅读(5389)  评论(0编辑  收藏  举报
操作简便,支付安全,轻松赚美元!