Java網(wǎng)絡(luò)通信問題總結(jié)

//ServerTest.java
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerTest {
    public static void main(String[] args){
        ServerSocket ss = null;
        Socket s = null;

        try{
            ss = new ServerSocket(2222);
        }catch(IOException e){
            e.printStackTrace();
            System.exit(-1);
        }


        while(true){
            try{
                s  = ss.accept();
                System.out.println("LocalPort:" + s.getLocalPort());
                System.out.println("Port:" + s.getPort());
                System.out.println("InetAddress:" + s.getInetAddress().getHostAddress());
                System.out.println("LocalAddress:" + s.getLocalAddress().getHostAddress());
                //只讀發(fā)來消息的線程
                new Thread(new MessageRead(s,"Client")).start();
                new Thread(new MessageWrite(s,"Server")).start();

            }catch (IOException e){
                e.printStackTrace();
                System.exit(-1);
            }
        }

       // ss.close();


    }
}

//NetTest.java
//java.lang.*;  math包 基本類型封裝對象 System Enum String* Thread* Exception*
//java.io.*;    流 RW流 Stream流 文件操作File
//java.util.*;  容器 date Iterator 泛型 list set map 計時器Timer
//java.sql.*;
//java.net.*;
//java.nio.*;


import java.io.*;
import java.net.Socket;

public class NetTest {
    public static void main(String[] args){
        Socket s = null;

        try{
            s = new Socket("127.0.0.1",2222);
            //只讀發(fā)來消息的線程
            new Thread(new MessageRead(s,"Server")).start();
            new Thread(new MessageWrite(s,"Client")).start();   //由此線程銷毀套接字

        }catch(IOException e){
            e.printStackTrace();
            System.exit(-1);
        }

    }
}
class MessageWrite implements Runnable {
    Socket s;
    String localName;

    MessageWrite(Socket s,String localName) {
        this.s = s;
        this.localName = localName;
    }

    @Override
    public void run() {
        DataOutputStream dos = null;
        BufferedReader br = null;

        try {
            dos = new DataOutputStream(s.getOutputStream());
            br = new BufferedReader(new InputStreamReader(System.in));
            String sb;

            while (!(sb = br.readLine()).equalsIgnoreCase("exit")) {

                System.out.print(localName + ":" + sb);
                System.out.println();

                dos.writeUTF(sb);
            }

            br.close();     //從輸入設(shè)備中讀入
            dos.flush();    //寫到Socket
            dos.close();    //關(guān)閉Socket流

            //讓消息發(fā)送對象接管Socket關(guān)閉
            s.close();      //關(guān)閉Socket
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }


    }
}
class MessageRead implements Runnable{
    Socket s;
    String remoteName;
    MessageRead(Socket s,String remoteName){
        this.s = s;
        this.remoteName = remoteName;
    }

    @Override
    public void run() {
        DataInputStream dis = null;

        try{
            dis = new DataInputStream(s.getInputStream());

            while(true){
                System.out.print(remoteName + ":" + dis.readUTF());
                System.out.println();
            }

          //  dis.close();    //從Socket讀入
        }catch(IOException e){
            e.printStackTrace();
            System.exit(-1);
        }

    }
}



//UDPRecive.java
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UDPRecive {
    public static void main(String[] args){
        byte[] buffer = new byte[1024];
        DatagramPacket dp = new DatagramPacket(buffer,buffer.length);
        DatagramSocket ds = null;

        DataInputStream dis = null;
        ByteArrayInputStream bais = null;
        try {
            ds = new DatagramSocket(6666);
        } catch (SocketException e) {
            e.printStackTrace();
        }

        while(true){
            try {
                ds.receive(dp);
            } catch (IOException e) {
                e.printStackTrace();
            }

           bais = new ByteArrayInputStream(buffer);
            dis = new DataInputStream(bais);


            try {
                System.out.println(dis.readLong());
            } catch (IOException e) {
                e.printStackTrace();
            }


        }
    }
}

//UDPSend.java
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;

public class UDPSend {
    public static void main(String[] args){

       ByteArrayOutputStream baos = new ByteArrayOutputStream();
       DataOutputStream dos = new DataOutputStream(baos);

        try {
            dos.writeLong(12345678900l);
        } catch (IOException e) {
            e.printStackTrace();
        }

        byte[] buffer = baos.toByteArray();

        DatagramPacket dp = new DatagramPacket(buffer,buffer.length,new InetSocketAddress("127.0.0.1",6666));
        DatagramSocket ds = null;
        try {
            ds = new DatagramSocket();
        } catch (SocketException e) {
            e.printStackTrace();
        }

        try {
            ds.send(dp);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ds.close();

    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容