一、网络编程——概述
网络通讯要素 IP地址 端口号 传输协议 两台机器相连接: 1、找到对方主机——通过IP; 2、数据发送给对应的端口(应用程序对应的数字标识): 数据要发送到对方指定的应用程序上,为了标识这些应用程序,所以给这些网络应用程序都用数字进行标识。为了方便称呼这个数字,把它叫做端口(逻辑端口)。 3、定义通信规则。这个通讯规则称为协议。 国际组织定义了通用协议 TCP/IP。
import java.net.*;
class IPDemo
{
public static void main(String[] args) throws UnknownHostException
{
InetAddress i = InetAddress.getLocalHost();
System.out.println(i.toString());
System.out.println("address: "+i.getHostAddress());
System.out.println("name: "+i.getHostName());
InetAddress[] iaa = InetAddress.getAllByName("www.baidu.com");
for(InetAddress ia : iaa)
{
System.out.println("address: "+ia.getHostAddress());//address: 112.80.248.74
System.out.println("name: "+ia.getHostName());//name: www.baidu.com
}
}
}
import java.net.*;
class UdpSend
{
public static void main(String[] args) throws Exception
{
//1、创建UDP服务,通过DatagramSocket对象,并设定一个端口号
DatagramSocket ds = new DatagramSocket(8888);
//2、提供数据,并封装成数据包
byte[] buf = "udp ge men lai le".getBytes();
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.254"),10000);
//创建数据包时,需要制定包的内容、内容的长度、目的主机、目的端口号
//3、通过socket服务,将已有的数据包发送出去,通过send方法。
ds.send(dp);
//4、关闭资源
ds.close();
}
}
import java.net.*;
class UdpReceive
{
public static void main(String[] args) throws Exception
{
//1、创建udpsocket,建立端点;
DatagramSocket ds = new DatagramSocket(10000);
//2、定义数据包,用于存储数据。
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
//3、通过服务的receive方法将收到的数据存入数据包中
ds.receive(dp);//该方法是阻塞式方法
//4、通过数据包的方法获取其中的数据
String ip = dp.getAddress().getHostAddress();
String data = new String(dp.getData(),0,dp.getLength());
int port = dp.getPort();
System.out.println(ip+" : "+port+" : "+data);
//5、关闭资源
ds.close();
}
}
import java.net.*;
import java.io.*;
class UdpSend
{
public static void main(String[] args) throws Exception
{
//创建DatagramSocket对象时,如果不指定端口,系统会默认分配一个端口。
DatagramSocket ds = new DatagramSocket();
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while(true)
{
line = bufr.readLine();
byte[] buf = line.getBytes();
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.254"),10001);
ds.send(dp);
if("886".equals(line))
break;
}
ds.close();
}
}
class UdpReceive
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds = new DatagramSocket(10001);
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
while(true)
{
ds.receive(dp);
String ip = dp.getAddress().getHostAddress();
int port = dp.getPort();
String data = new String(dp.getData(),0,dp.getLength());
System.out.println(ip+" : "+port+" : "+data);
if("886".equals(data))
break;
}
ds.close();
}
}
import java.net.*;
import java.io.*;
class Send implements Runnable
{
private DatagramSocket ds;
Send(DatagramSocket ds)
{
this.ds = ds;
}
public void run()
{
try
{
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while(true)
{
line = bufr.readLine();
if("886".equals(line))
break;
byte[] buf = line.getBytes();
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.254"),10002);
ds.send(dp);
}
}
catch (Exception e)
{
throw new RuntimeException("发送端失败");
}
ds.close();
}
}
class Receive implements Runnable
{
private DatagramSocket ds;
Receive(DatagramSocket ds)
{
this.ds = ds;
}
public void run()
{
try
{
while(true)
{
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
ds.receive(dp);
String ip = dp.getAddress().getHostAddress();
String data = new String(dp.getData(),0,dp.getLength());
System.out.println(ip+"::"+data);
}
}
catch (Exception e)
{
throw new RuntimeException("接收端失败");
}
}
}
class CharDemo
{
public static void main(String[] args) throws Exception
{
DatagramSocket sendSocket = new DatagramSocket();
DatagramSocket receSocket = new DatagramSocket(10002);
new Thread(new Send(sendSocket)).start();
new Thread(new Receive(receSocket)).start();
}
}
import java.io.*;
import java.net.*;
class TcpClient
{
public static void main(String[] args) throws Exception
{
//创建客户端的Socket服务,指定目的主机和端口。
Socket s = new Socket("192.168.1.254",10003);
//为了发送数据,应该获取socket流中的OutputStream
OutputStream out = s.getOutputStream();
out.write("TCP 哥们来了".getBytes());
s.close();
}
}
import java.io.*;
import java.net.*;
class TcpServer
{
public static void main(String[] args) throws Exception
{
//建立服务端的socket服务,并监听一个端口
ServerSocket ss = new ServerSocket(10003);
//通过accept方法获取链接过来的客户端对象。
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println("ip: "+ip);
//获取客户端发送多来的数据,那么要使用客户端对象的读取流来读取数据。
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
String str = new String(buf,0,len);
System.out.println(str);
//关闭资源
s.close();
ss.close();
}
}
import java.io.*;
import java.net.*;
class TcpClient2
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("192.168.1.254",10004);
OutputStream out = s.getOutputStream();
out.write("服务端,你好!".getBytes());
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
s.close();
}
}
class TcpServer2
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(10004);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println("ip: "+ip);
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
OutputStream out = s.getOutputStream();
Thread.sleep(4000);
out.write("收到".getBytes());
s.close();
ss.close();
}
}
import java.io.*;
import java.net.*;
class TransClient
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("192.168.1.254",10005);
//定义读取键盘数据的流对象
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
//定义目的,将数据写入到socket输出流,发给服务端
BufferedWriter bufOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//PrintWriter out = new PrintWriter(s.getOutoutStream(),true);
//定义一个socket读取流,读取服务端返回的大写信息。
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = null;
while(true)
{
line = bufr.readLine();
bufOut.write(line);
bufOut.newLine();
bufOut.flush();
//out.println(line);
if("over".equals(line))
break;
String str = bufIn.readLine();
System.out.println("server: "+str);
}
bufr.close();
s.close();
}
}
/*
服务端:
源:Socket读取流
目的:socket输出流
*/
class TransServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(10005);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println("ip:"+ip);
//读取socket读取流中的数据。
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
//目的:socket输出流,将大写数据写入到socket输出流,并发送给客户端。
BufferedWriter bufOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//PrintWriter out = new PrintWriter(s.getOutputStream(),true);
String line = null;
while(true)
{
line = bufIn.readLine();
if("over".equals(line))
break;
bufOut.write(line.toUpperCase());
bufOut.newLine();
bufOut.flush();
//out.println(line.toUpperCase());
}
s.close();
ss.close();
}
}
import java.io.*;
import java.net.*;
class Client
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("192.168.1.254",10006);
BufferedReader bufr = new BufferedReader(new FileReader("IPDemo.java"));
//BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
String line = null;
while((line=bufr.readLine())!=null)
{
//bufw.write(line);
//bufw.newLine();
//bufw.flush();
pw.println(line);
}
s.shutdownOutput();//定义结束标记
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = bufIn.readLine();
System.out.println(str);
bufr.close();
s.close();
}
}
class Server
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(10006);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"......connected");
BufferedReader bufr = new BufferedReader(new InputStreamReader(s.getInputStream()));
//BufferedWriter bufw = new BufferedWriter(new FileWriter("copy_IPDemo.txt"));
PrintWriter pw = new PrintWriter(new FileWriter("server.txt"),true);
String line = null;
while((line=bufr.readLine())!=null)//当客户端读到s.shutdownOutput();时,这里才读到null。
{
//bufw.write(line);
//bufw.newLine();
//bufw.flush();
pw.println(line);
}
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
out.println("上传成功");
//bufw.close();
pw.close();
s.close();
ss.close();
}
}