网络编程
概述
计算机网络:
计算机网络是通过传输介质、通信设施和网 络通信协议,将分散在不同地点的计算机设备互联起来实现资源共享和数据传输的计算机系统。
网络通信要素
IP: InetAdress类
根据域名得到对应的IP地址对象,通过getByName(String host)静态方法得到该类的一个实例
public static void main(String[] args) {
try {
InetAddress ip = InetAddress.getByName("www.baidu.com");
System.out.println(ip);
System.out.println(ip.getHostAddress());
System.out.println(ip.getHostName());
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
端口
端口表示计算机上的一个程序的进程;
通信协议
TCP/IP协议簇
聊天实现
TCP
服务器端:
- 建立服务的端口
- 等待用户的连接
- 接收用户消息
public class TcpServerDemo01 {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
serverSocket = new ServerSocket(9999);
socket = serverSocket.accept();
is = socket.getInputStream();
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
is.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端:
-
连接服务器 -
发送消息
public class TcpClientDemo01 {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
try {
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 9999;
socket = new Socket(serverIP, port);
os = socket.getOutputStream();
os.write("你好,我来了!".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
os.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
UDP
接收方
public class UdpReceiveDemo01 {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(9900);
byte[] bytes = new byte[1024];
DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);
socket.receive(packet);
System.out.println(new String(packet.getData(),0,packet.getLength()));
socket.close();
}
}
发送方
public class UdpSenderDemo01 {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket();
String msg="虹猫你好";
InetAddress ip = InetAddress.getByName("127.0.0.1");
int port = 9900;
DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,ip,port);
socket.send(packet);
socket.close();
}
}
文件上传实现
服务器端
public class TcpServerDemo02 {
public static void main(String[] args) throws IOException {
InetAddress ip = InetAddress.getByName("127.0.0.1");
ServerSocket serverSocket = new ServerSocket(9900,10, ip);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("1.rar"));
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
客户端
public class TcpClientDemo02 {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1",9900);
OutputStream os = socket.getOutputStream();
FileInputStream fis = new FileInputStream(new File("E:\\demo\\demo\\1.rar"));
byte[] buffer = new byte[1024];
int len;
while ((len=fis.read(buffer))!=-1){
os.write(buffer,0,len);
}
socket.shutdownOutput();
fis.close();
os.close();
socket.close();
}
}
推荐java全栈学习视频,个人感觉很nice 狂神—>学习链接跳转路径
|