java基础教程-网络编程(七)

七、网络编程

7.1.IP协议

  最重要的贡献是IP地址

7.2.TCP和UDP协议

  TCP(快)可靠传输,发送时必须建立连接(三次握手协议 )

  UDP(慢)不可靠传输,发送时无须建立连接

7.3.TCP 的TCP Server和TCP Client

     

  注意,要首先启动Server,再启动Client

  端口号是应用的入口,有两个字节,所以每个服务器最多能运行65536应用程序,而TCP的端口和UDP的端口不一样,定义端口时,尽量定义1024以上的

  特别典型的端口号,http  80

//TCP Server

import java.net.*;

import java.io.*;

 

public class TCPServer {

public static void main(String[] args) throws Exception {

ServerSocket ss = new ServerSocket(6666);

while(true) {

Socket s = ss.accept();//阻塞函数,一直在这等,直到有访问,才会继续执行

System.out.println("a client connect!");

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

System.out.println(dis.readUTF());

dis.close();

s.close();

}

 

}

}

 

 

//TCP Client

import java.net.*;

import java.io.*;

 

public class TCPClient {

public static void main(String[] args) throws Exception {

Socket s = new Socket("127.0.0.1", 6666);

OutputStream os = s.getOutputStream();

DataOutputStream dos = new DataOutputStream(os);

Thread.sleep(3000);

dos.writeUTF("hello server!");

dos.flush();

dos.close();

s.close();

}

}

 

  

7.4.UDP   Server和Client(UDP只能传送字节数组)

//UDP   Server

import java.net.*;

 

public class TestUDPServer

{

public static void main(String args[]) throws Exception

{

byte buf[] = new byte[1024];

DatagramPacket dp = new DatagramPacket(buf, buf.length);

DatagramSocket ds = new DatagramSocket(5678);

while(true)

{

ds.receive(dp);

System.out.println(new String(buf,0,dp.getLength()));//String的构造函数

}

}

}

   

//UDP Client

import java.net.*;

 

public class TestUDPClient

{

public static void main(String args[]) throws Exception

{

byte[] buf = (new String("Hello")).getBytes();

DatagramPacket dp = new DatagramPacket(buf, buf.length, 

   new InetSocketAddress("127.0.0.1", 5678)

   );

DatagramSocket ds = new DatagramSocket(9999);//client自己的端口,因为UDP不是面向连接的

ds.send(dp);

ds.close();

}

}

  

//TestUDP Client

import java.net.*;

import java.io.*;

 

public class TestUDPClient

{

public static void main(String args[]) throws Exception

{

long n = 10000L;

ByteArrayOutputStream baos = new ByteArrayOutputStream();

DataOutputStream dos = new DataOutputStream(baos);

dos.writeLong(n);

 

byte[] buf = baos.toByteArray();//将long型数据转化成字符数组的形式,进行输入

System.out.println(buf.length);

 

DatagramPacket dp = new DatagramPacket(buf, buf.length, 

   new InetSocketAddress("127.0.0.1", 5678)

   );

DatagramSocket ds = new DatagramSocket(9999);

ds.send(dp);

ds.close();

 

}

}

 

  

//TestUDP Server

import java.net.*;

import java.io.*;

 

public class TestUDPServer

{

public static void main(String args[]) throws Exception

{

byte buf[] = new byte[1024];

DatagramPacket dp = new DatagramPacket(buf, buf.length);

DatagramSocket ds = new DatagramSocket(5678);

while(true)

{

ds.receive(dp);

ByteArrayInputStream bais = new ByteArrayInputStream(buf);

DataInputStream dis = new DataInputStream(bais);

System.out.println(dis.readLong());//将字符数组读成long型

}

}

}

  

原文地址:https://www.cnblogs.com/likailiche/p/5133373.html