1. 程式人生 > >socket程式設計 java實現

socket程式設計 java實現

 先從伺服器端說起。伺服器端先初始化Socket,然後與埠繫結(bind),對埠進行監聽(listen),呼叫accept阻塞,等待客戶端連線。在這時如果有個客戶端初始化一個Socket,然後連線伺服器(connect),如果連線成功,這時客戶端與伺服器端的連線就建立了。客戶端傳送資料請求,伺服器端接收請求並處理請求,然後把迴應資料傳送給客戶端,客戶端讀取資料,最後關閉連線,一次互動結束。

獲取主機資訊

package SYA;
import java.net.*;
public class Host 
{
	public InetAddress ipa;
	public Host()
	{
		try
		{
			ipa=InetAddress.getByName(null);//獲取本機的地址
			//System.out.println("Host Name:"+ipa.getHostName());
			//System.out.println("Host IP:"+ipa.toString());
			//System.out.println("Local Host Name:"+ipa.getLocalHost());
		}
		catch(UnknownHostException e)
		{
			System.out.println(e.toString());
		}
	}

}

客戶端程式

package SYA;

import java.io.*;

import java.net.*;


public class Client 
{
	public static void main(String[] args)
	{
		System.out.println("我是客戶端");
		String str;
		try
		{
			Host h=new Host();
			Socket socket=new Socket(h.ipa,8000);
			System.out.println("Socket:"+socket);
			InputStream fin=socket.getInputStream();
			OutputStream fout=socket.getOutputStream();
			//建立資料流
			InputStreamReader in=new InputStreamReader(fin);
			BufferedReader jin=new BufferedReader(in);
			PrintStream out=new PrintStream(fout);
			InputStreamReader in_out=new InputStreamReader(System.in);
			BufferedReader jout=new BufferedReader(in_out);
			while(true)
			{
				System.out.println("傳送字串");
				str=jout.readLine();
				out.println(str);
				if(str.equals("end"))break;
				System.out.println("等待伺服器訊息");
				str=jin.readLine();
				System.out.println("伺服器訊息:"+str);
				if(str.equals("end"))break;
			}
		}
		catch(Exception e)
		{
			
		}
		
	} 
	 public void fun ()
	 {
		  try {
			  Socket s= new Socket("localhost",7522);
			  OutputStream os= (OutputStream) s.getOutputStream();
			  OutputStreamWriter osw=new OutputStreamWriter(os);
			  PrintWriter pw= new PrintWriter(osw,true);
			  pw.println("你好");
			  InputStream is = s.getInputStream();
			  InputStreamReader isw	= new InputStreamReader (is);
			 BufferedReader br= new BufferedReader(isw);
			 
			 String mess= br.readLine();
			 System.out.println(mess);
		  }catch( Exception e ){
			  
		  }
	  }
}

伺服器端程式

package SYA;
import java.net.*;
import java.io.*;

public class Server 
{
	public static final int port=8000;
	public static void main(String[] args)
	{
		String str;
		System.out.println("我是服務端");
		try
		{
			 ServerSocket server=new ServerSocket(port);//建立執行緒監聽物件
			 System.out.println("started:"+server);
			 Socket socket=server.accept();//負責C/S通訊的socket物件
			 System.out.println("Socket:"+socket);
			 //socket的輸入輸出流
			 InputStream fin=socket.getInputStream();
			 OutputStream fout=socket.getOutputStream();
			 //C/S資料流
			 InputStreamReader in=new InputStreamReader(fin);
			 BufferedReader jin=new BufferedReader(in);
			 //S/C輸出
			 PrintStream out= new PrintStream(fout);
			 InputStreamReader in_out=new InputStreamReader(System.in);
			 BufferedReader jout=new BufferedReader(in_out);
			 while(true)
			 {
				 System.out.println("等待客戶端訊息");
				 str=jin.readLine();
				 System.out.println("客戶端輸入:"+str);
				 if(str.equals("end")){break;}
				 System.out.print("服務端傳送:");
				 str=jout.readLine();
				 out.println(str);//向客戶端傳送
				 if(str.equals("end")){break;}
			 }
			 socket.close();
		}
		catch(Exception e)
		{
			System.out.println("異常");
		}
		
	}
	public void fun()
	{
		try{
			ServerSocket ss=new ServerSocket (7522);//7522是埠號,我們設計埠號要避開8080,1433,1521,3306這幾個埠
			System.out.println("監聽前");
			Socket s=ss.accept();//伺服器端執行到這會停下,等待客戶端
			/*System.out.println(s.getInputStream().read());*/
			InputStream is= s.getInputStream();
			InputStreamReader isr=new InputStreamReader(is);
			BufferedReader br=new BufferedReader (isr);//位元組流轉換成字元流
			String name=br.readLine();
			System.out.println(name);
			OutputStream os=  s.getOutputStream();
			OutputStreamWriter osw=new OutputStreamWriter(os);
			PrintWriter pw=new PrintWriter(osw,true);
			pw.println("welcom"+name);
			
		}catch (Exception e){
			
		}
	}
	

}

執行結果: