【java】网络编程-创新互联
文章目录
本文标题:【java】网络编程-创新互联
本文地址:http://lswzjz.com/article/depigj.html
- 网络编程概述
- 基本概念
- IP地址概念
- InetAddress
- 端口与协议概念
- UDP通信编程
- UDP发送数据
- UDP接受数据
- UDP通信程序练习
- TCP通信编程
- TCP发送数据
- TCP接收数据
- TCP通信程序练习
终端检查:
package heima.网络编程;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class inetaddress {public static void main(String[] args) throws UnknownHostException {//public static InetAddress getByName(String host):确定主机名称的IP地址,主机名称可以是机器名称,也可以是IP地址
// InetAddress address = InetAddress.getByName("ludan");
InetAddress address = InetAddress.getByName("192.168.3.55");
//public String getHostName():获取此IP地址的主机名。
String name = address.getHostName();
//public String getHostAddress():返回文本显示中的IP地址字符串。
String ip = address.getHostAddress();
System.out.println("主机名:"+name);
System.out.println("IP地址:"+ip);
}
}
端口与协议概念package heima.网络编程;
import java.io.IOException;
import java.net.*;
public class udp {public static void main(String[] args) throws IOException {//创建发送端的Socket对象(DatagramSocket)
//public DatagramSocket() 构造数据报套接字并将其绑定到本地主机上的任何可用端口。
DatagramSocket ds = new DatagramSocket();
//创建数据,并把数据打包
//DatagramPacket (byte[] buf,int length,InetAddress address,int part)
//构造一个数据包,发送长度为length的数据包到指定主机上的指定端口号。
byte[] bys = "hello,udp,我来了".getBytes();
DatagramPacket dp = new DatagramPacket(bys, bys.length, InetAddress.getByName("192.168.3.55"),8080);
//way2
// int length = bys.length;
// InetAddress address = InetAddress.getByName("192.168.3.55");
// int part = 10086;
// DatagramPacket dp = new DatagramPacket(bys,length,address,part);
//调用DatagramSocket对象的方法发送数据
//void send(DatagramSocket p) 从此套接字发送数据包
ds.send(dp);
//关闭发送端
//void vlose() 关闭此数据报套接字
ds.close();
}
}
UDP接受数据package heima.网络编程;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class ReceiveUDP {public static void main(String[] args) throws IOException { //参加接受端的Socket对象(DatagramSocket)
//DatagramSocket (int part) 构造数据报套接字并将其绑定到本地主机上的指定端口
DatagramSocket ds = new DatagramSocket(8080);
//创建一个数据包,用于接收数据
//DatagramSocket(byte[] buf,int length) 构造一个DatagramPacket用于接收长度为length数据
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys, bys.length);
//调用DatagramSocket对象的方法接收数据
ds.receive(dp);
//解析数据包,并把数在控制台显示
//byte[] getData() 返回数据缓冲区
// byte[] datas = dp.getData();
//int getlength() 返回要发送的数据长度或接受到的数据的长度
// int len = dp.getLength();
// String dataString = new String(datas,0,len);
// System.out.println("数据是:"+dataString);
System.out.println("数据是:"+new String(dp.getData(),0,dp.getLength()));
//关闭接收端
ds.close();
}
}
UDP通信程序练习很奇怪,接受不到数据。。
创建了一个item03的路径,在此路径下:
udp1.java
package heima.网络编程.item03;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class udp1 {public static void main(String[] args) throws IOException {//创建发送端的Socket对象(DatagramSocket)
DatagramSocket ds = new DatagramSocket();
//自己封装键盘录入数据
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = br.readLine())!=null){//输入的数据是886,发送数据结束
if ("886".equals(line)){break;
}
//创建数据,并把数据打包
byte[] bys = line.getBytes();
DatagramPacket dp = new DatagramPacket(bys, bys.length, InetAddress.getByName("192.168.3.55"),12345);
//调用DatagramPacket对象的方法发送数据
ds.send(dp);
}
//关闭发送源
ds.close();
}
}
Receive.java
package heima.网络编程.item03;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class Receive1 {public static void main(String[] args) throws IOException {//创建接收端Socket对象(DatagramSocket)
DatagramSocket ds = new DatagramSocket(12345);
while (true){//创建一个数据包,用于接收数据
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys, bys.length);
//调用DatagramSocket对象的方法接收数据
ds.receive(dp);
//解析数据包,并把数据在控制台显示
System.out.println("数据是:"+new String(dp.getData(),0,dp.getLength()));
}
// //关闭接收端
// ds.close();
//因为要一直接收数据,所以它也不用关了
}
}
TCP通信编程
TCP发送数据package heima.网络编程.item04;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class ClientDemo {public static void main(String[] args) throws IOException {//创建客户端的Socket对象(Socket)
//Socket(InetAddress address,int part) 创建流套接字并将其连接到指定的IP地址的指定端口号
// Socket s = new Socket(InetAddress.getByName("192.168.3.55"),10000);
//Socket(String host,int part)创建流套接字并将其连接到指定主机上的指定端口号
Socket s = new Socket("192.168.3.55",10000);
//获取输出流,写数据
//OutputStream getOutputStream() 返回此套接字的输出流
OutputStream os = s.getOutputStream();
os.write("hello,tcp,我来了".getBytes());
//释放资源
s.close();
}
}
TCP接收数据现在运行会报错,是因为还缺少TCP数据的接收
package heima.网络编程.item04;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class ClientDemo {public static void main(String[] args) throws IOException {//创建客户端的Socket对象(Socket)
//Socket(InetAddress address,int part) 创建流套接字并将其连接到指定的IP地址的指定端口号
// Socket s = new Socket(InetAddress.getByName("192.168.3.55"),10000);
//Socket(String host,int part)创建流套接字并将其连接到指定主机上的指定端口号
Socket s = new Socket("192.168.3.55",12345);
//获取输出流,写数据
//OutputStream getOutputStream() 返回此套接字的输出流
OutputStream os = s.getOutputStream();
os.write("hello,tcp,我来了".getBytes());
//释放资源
s.close();
}
}
TCP通信程序练习- 练习1 --服务器给出反馈
package heima.网络编程.P349;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class ClientDemo {public static void main(String[] args) throws IOException {//创建客户端的Socket对象
Socket s = new Socket("192.168.3.55",12345);
//获取输出流,写数据
OutputStream os = s.getOutputStream();
os.write("hello,tcp,我来了".getBytes());
//接受服务器反馈
InputStream is = s.getInputStream();
byte[] bys = new byte[1024];
int len = is.read(bys);
String data = new String(bys,0,len);
System.out.println("客户端:"+data);
//释放资源
s.close();
}
}
package heima.网络编程.P349;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {public static void main(String[] args) throws IOException {//创建服务端的Socket对象
ServerSocket ss = new ServerSocket(12345);
//监听客户端连接,返回一个Socket对象
Socket s = ss.accept();
//获取输入流,读数据,并把数据显示在控制台
InputStream is = s.getInputStream();
byte[] bys = new byte[1024];
int len = is.read(bys);
String data = new String(bys,0,len);
System.out.println("服务器:"+data);
//给出反馈
OutputStream os = s.getOutputStream();
os.write("数据已收到".getBytes());
//释放资源
ss.close();
}
}
注:
- 只释放Socket对象就ok了,其他不用再释放
- 端口报错,就换一个端口
- 练习2 --客户数据来自于键盘录入
package heima.网络编程.TPC练习2;
import java.io.*;
import java.net.Socket;
public class ClientDemo {public static void main(String[] args) throws IOException {Socket s = new Socket("192.168.3.55",12345);
//数据来自于键盘录入,知道输入的是886,发送数据结束
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//封装输出流对象
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while ((line= br.readLine())!=null){if ("886".equals(line)){break;
}
//获取输出流对象
bw.write(line);
bw.newLine();
bw.flush();
}
//释放资源4
s.close();
}
}
package heima.网络编程.TPC练习2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {public static void main(String[] args) throws IOException {//创建服务器Socket对象
ServerSocket ss = new ServerSocket(12345);
//监听客户端的连接,返回一个对应的Socket对象
Socket s = ss.accept();
//获取输入流
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line;
while ((line = br.readLine())!=null){System.out.println(line);
}
//释放资源
ss.close();
}
}
在ClientDemo.java终端输入,则在ServerDemo处输出,知道输入886为止
- 练习3 --服务器数据写入文本文件
package heima.网络编程.TPC练习3;
import java.io.*;
import java.net.Socket;
public class ClientDemo {public static void main(String[] args) throws IOException {Socket s = new Socket("192.168.3.55",12345);
//数据来自于键盘录入,知道输入的是886,发送数据结束
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//封装输出流对象
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while ((line=br.readLine())!=null){if ("886".equals(line)){break;
}
//获取输出流对象
bw.write(line);
bw.newLine();
bw.flush();
}
//释放资源
s.close();
}
}
package heima.网络编程.TPC练习3;
import kuang.P70.B;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {public static void main(String[] args) throws IOException {//创建服务器Socket对象
ServerSocket ss =new ServerSocket(12345);
//监听客户端连接,返回一个对应的Socket对象
Socket s = ss.accept();
//接收数据
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//把数据写入文本文件
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\路聃\\Desktop\\Java\\s.txt"));
String line;
while ((line= br.readLine())!=null){bw.write(line);
bw.newLine();
bw.flush();
}
//释放资源
bw.close();
ss.close();
}
}
输出:
- 练习4 --客户端数据来自文本文件
莫名出错
package heima.网络编程.TPC练习4;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {public static void main(String[] args) throws IOException {ServerSocket ss = new ServerSocket(12345);
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\路聃\\Desktop\\Java\\1.txt"));
String line;
while ((line= br.readLine())!=null){bw.write(line);
bw.newLine();
bw.flush();
}
//释放资源
bw.close();
ss.close();
}
}
package heima.网络编程.TPC练习4;
import java.io.*;
import java.net.Socket;
public class ClientDemo {public static void main(String[] args) throws IOException {Socket s =new Socket("192.168.3.55",12345);
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\路聃\\Desktop\\Java\\s.txt"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while ((line= br.readLine())!=null){bw.write(line);
bw.newLine();
bw.flush();
}
//释放资源
br.close();
s.close();
}
}
- 练习5 --上传文件服务器给出的反馈
package heima.网络编程.TPC练习5;
import java.io.*;
import java.net.Socket;
public class ClientDemo {public static void main(String[] args) throws IOException {Socket s = new Socket("192.168.3.55",10000);
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\路聃\\Desktop\\Java\\s.txt"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;;
while ((line= br.readLine())!=null){bw.write(line);
bw.newLine();
bw.flush();
}
//public void shutdownOutput() --非自定义结束标记
s.shutdownInput();
//接收反馈
BufferedReader brClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
String data = brClient.readLine();
System.out.println("服务器的反馈:"+data);
//释放资源
br.close();
s.close();
}
}
package heima.网络编程.TPC练习5;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {public static void main(String[] args) throws IOException {ServerSocket ss = new ServerSocket(10000);
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\路聃\\Desktop\\Java\\Copy.txt"));
String line;
while ((line= br.readLine())!=null){bw.write(line);
bw.newLine();
bw.flush();
}
//给出反馈
BufferedWriter bwServer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bwServer.write("文件上传成功");
bwServer.newLine();
bwServer.flush();
}
}
连接端口失败,原因未知
- 练习6 --多线陈实现文件上传
package heima.网络编程.TPC练习6;
import java.io.*;
import java.net.Socket;
public class ClientDemo {public static void main(String[] args) throws IOException {Socket s = new Socket("192.168.3.55",12345);
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\路聃\\Desktop\\Java\\s.txt"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;;
while ((line= br.readLine())!=null){bw.write(line);
bw.newLine();
bw.flush();
}
//public void shutdownOutput() --非自定义结束标记
s.shutdownInput();
//接收反馈
BufferedReader brClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
String data = brClient.readLine();
System.out.println("服务器的反馈:"+data);
//释放资源
br.close();
s.close();
}
}
package heima.网络编程.TPC练习6;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {public static void main(String[] args) throws IOException {//创建服务器Socket对象
ServerSocket ss = new ServerSocket(12345);
while (true){//监听客户端连接,返回一个对应的Socket对象
Socket s = ss.accept();
//为每一个客户端开启一个线程
new Thread(new ServerThread(s)).start();
}
//为什么不释放资源了?
}
}
package heima.网络编程.TPC练习6;
import java.io.*;
import java.net.Socket;
public class ServerThread implements Runnable{private Socket s;
public ServerThread(Socket s){this.s = s;}
@Override
public void run() {try {//接收数据写到文本文件
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//解决名称冲突问题
int count = 0;
File file = new File("C:\\Users\\路聃\\Desktop\\Java\\Copy["+count+"].java");
while (file.exists()){count++;
file = new File("C:\\Users\\路聃\\Desktop\\Java\\Copy["+count+"].java");
}
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
String line;
while ((line= br.readLine())!=null){bw.write(line);
bw.newLine();
bw.flush();
}
} catch (IOException e) {e.printStackTrace();
}
}
}
依然显示连接失败。。。
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
本文标题:【java】网络编程-创新互联
本文地址:http://lswzjz.com/article/depigj.html