1. 程式人生 > >談一談socket與java

談一談socket與java

res socket 解決方案 sts 構造 ron sta bsp 直接

用java中socket實現socket聊天

1, 什麽是socket

Socket 是指網絡套接字,什麽是套接字呢? 這是網絡上一種端對端的網絡協議,端口就是進程號,socket在網絡中讓這兩個端通信形成端口直接通信,所以socket的參數可想而知就是兩端ip和端口號了;

再說在網絡中,總要有人付出,要在網絡中等著別人鏈接,不然的話你一點想連別人,別人在兩點想連你,怎麽也不可能連上,總有喲個人需要做等待的角色,這個角色就是服務端也就是serverSocket,他在網絡中提供一個固定的ip和端口號套接字;

2, 在Java中實現socket接口

在Java中提供了兩個用來建立socket鏈接都在java.net包中,在客戶端要用Socket(addr,port)//參數是遠程服務的地址和端口號;另一個在服務端中用ServerSocket(port) 建立一個等待的socket端,同時使用serversocket.accept() 在服務端動態等待並接受一個前來請求鏈接的socket請求

一旦serversocket.accept()方法獲得一個鏈接,就會封裝出一個Socket對象服務端可用這個對象,獲得客戶端的信息並向客戶端發送信息;話不多說,上代碼;

  

package testpackage;
/*
 * @author:mayeye
 * @about:服務端
 * */
import java.net.*;
import java.io.*;

public class TestServer {
    public static void main(String[] args) {
        //try {
            System.out.println("=============================");
            ServerSocket server
=null; try { server=new ServerSocket(1233); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Socket client = null; try { client = server.accept(); System.out.println(
"ip為:"+client.getInetAddress()+"的客戶端鏈接了"); System.out.println("port為:"+client.getPort()+"的客戶端鏈接了"); System.out.println("Localport為:"+client.getLocalPort()+"的客戶端鏈接了"); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } String line = null; BufferedReader is = null; try { is = new BufferedReader(new InputStreamReader(client.getInputStream())); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } PrintWriter os = null; try { os = new PrintWriter(client.getOutputStream()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } BufferedReader sin=new BufferedReader(new InputStreamReader(System.in)); try { System.out.println("Client:"+is.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { line=sin.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } while(!line.equals("bye")){ os.println(line); os.flush(); System.out.println("Server:"+line); try { System.out.println("Client:"+is.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { line=sin.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } os.close(); try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { client.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { server.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("********************************"); /*}catch(Exception e) { System.out.println("-------------------------------"); }*/ } }

上面是服務端代碼

package testpackage;
import java.net.*;
import java.io.*;
public class testsocket {
    public static void main(String[] args) {
        try {
            Socket client=new Socket("127.0.0.1",1233);
            BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));//構建一個標準輸入流
            PrintWriter os=new PrintWriter(client.getOutputStream());
            BufferedReader is=new BufferedReader(new InputStreamReader(client.getInputStream()));//由client對象構造一個輸入流
            String readline;
            System.out.println("ip為:"+client.getInetAddress()+"的客戶端鏈接了");
            readline=sin.readLine();
            while(!readline.equals("bye")) {
                os.println(readline);
                os.flush();
                System.out.println("Client:"+readline);
                System.out.println("---*****************---");
                System.out.println("Server:"+is.readLine()); 
                readline=sin.readLine(); //從系統標準輸入讀入一字符串s
            }
            os.close();
            is.close();
            client.close();

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            System.out.print("====================找不到主機名");
        } catch (IOException e) {
            System.out.print("===================IO錯誤");
        }
        
    }
}

上面是服務端代碼;

大家都是成年人,所以目錄結構就不用我說了吧

這只是用java實現了一個最簡易的socket聊天,必須一問一答,而且沒有界面;

預告一下,下一篇博客我將接介紹界面的解決方案;

談一談socket與java