您的位置:首页 > 理论基础 > 计算机网络

网络编程部分案例总结

2015-12-16 16:32 741 查看
网络编程部分案例总结

1.网络编程的三要素
A:IP地址
B:端口
是应用程序的标示。范围:0-65535,其中0-1024被系统占用或保留,不建议使用
C:协议
UDP:数据打包传输,包大小有限制,不连接,效率高,传输中可能会丢包,不可靠
TCP:建立数据传输通道,数据大小无限制,效率低,可靠

2.UDP
A:最基本的UDP协议发送和接收数据
发送端:
public class SendDemo{
public static void main(String[] args) throws IOException{
//创建发送端Socket对象
DatagramSocket ds = new DatagramSocket();
//创建IP地址对象
InetAddress address = InetAddress.getByName("192.168.1.124");
//创建数据并打包
byte[] bys = "hello,我来了!".getBytes();
int length = bys.length;
int port = 10088;
DatagramPacket dp = new DatagramPacket(bys,length,address,port);
//调用Socket对象的发送端发送数据
ds.send(dp);
//释放资源
ds.close();
}
}
发送端简化版:
public class SendDemo{
public static void main(String[] args) throws IOException {
//创建发送端Socket对象
DatagramSocket ds = new DatagramSocket();
//创建数据并打包
byte[] bys = "hello,大家好!".getBytes();
DatagramPacket dp = new DatagramPacket(bys,bys.length,
InetAddress.
4000
getByName("192.168.1.124"),12345);
//发送数据
ds.send(dp);
//释放资源
ds.close();

}
}

接收端:
public class ReceiveDemo{
public static void main(String[] args) throws IOException{
//创建接收端Socket对象
DatagramSocket ds = new DatagramSocket(10088);
//创建一个接收数据包(接收容器)
byte[] bys = new byte[1024];
int length = bys.length;
DatagramPacket dp = new DatagramPacket(bys,length);
//调用Socket对象的接收方法接收数据
ds.receive(dp);
//解析数据包并显示在控制台上
InetAddress address = dp.getAddress();
String IP = address.getHostAddress();
byte[] bys2 = dp.getData();
int len = dp.getLength();
String str = new String(bys2,o,len);
System.out.println(IP +"传递的数据是:"+ str);
//释放资源
ds.close();
}
}
接收端简化版:
public class ReceiveDemo{
public static void main(String[] args) throws IOException {
//创建接收端Socket对象
DatagramSocket ds = new DatagramSocket(12345);
//创建一个接收包
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys,bys.length);
//接收数据
ds.receive(dp);
//解析数据并打印
String IP = dp.getAddress().getHostAddress();
String str = new String(dp.getData(),0,dp.getLength());
System.out.println(IP + "传递的数据是:" + str);
//释放资源
ds.close();
}
}

B:键盘录入数据发送和接收
发送端:
public calss SendDemo{
public static void main(String[] args) throws IOException {
//创建发送端Socket对象
DatagramSocket ds = new DatagramSocket();
//封装键盘录入数据
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line = br.readLine()) != null){
if("886".equals(line)){
break;
}
//创建数据并打包
byte[] bys = line.getBytes();
DatagramPacket dp = new DatagramPacket(bys,bys.length,
InetAddress.getByName("192.168.1.124"),10011);
//发送数据
ds.send(dp);
}
//关闭资源
}
}

接收端:
public calss ReceiveDemo{
public static void main(String[] args) throws IOException{
//创建接收端Socket对象
DatagramSocket ds = new DatagramSocket(10011);

while(true){
//创建接收包
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys,bys.length);
//接收数据
ds.receive(dp);
//解析数据
String IP = dp.getAdress().getHostAdress();
String str = new String(dp.getData(),0,dp.getLength());
System.out.println(IP + "传输的数据是:" + str)
}
}
}

C:使用多线程实现同一个窗口下数据的接收和发送(模拟聊天窗口)
数据发送线程:
public class SendThread implements Runnable{
private DatagramSocket ds;
public SendThread(DatagramSocket ds){
this.ds = ds;
}
public void run(){
try{
//封装键盘录入数据
BufferedReader  br = new BufferedReader(new
InputStreamReader(System.in));

String line = null;
while((line = br.readLine()) != null){
if("886".equals(line)){
break;
}
//创建数据并打包
byte[] bys = line.getBytes();
DatagramPacket dp = new DatagramPacket(bys,bys.length,
InetAddress.getByName("192.168.1.124"),10068);
//发送数据
ds.send(dp);
}
//释放资源
ds.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
数据接收线程:
public class ReceiveThread implements Runnable {
private DatagramSocket ds;
pubic ReceiveThread(DatagramSocket ds){
this.ds = ds;
}

public void run(){
try{
while(true){
//创建一个接收包
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys,bys.length);
//接收数据
ds.receive(dp);
//解析数据
String IP = dp.getAddress().getHostAddress();
String str = new String(dp.getDate(),0,dp.getLength());
System.out.println(IP + " : " + str);
}
}catch(IOException e){
e.printStackTrace();
}
}
}
聊天室:
public class ChatRoom{
public static void main(String[] args) throws IOException{
//创建发送端和接收端的Socket对象
DatagramSocket dsSend = new DatagramSocket();
DategramSocket dsReceive = new DatagramSocket(12345);
//创建发送端和接收端的资源对象
SendThread st = new SendThread(dsSend);
ReceiveThread rt = new ReceiveThread(dsReceive);
//创建线程
Thread t1 = new Thread(st);
Thread t2 = new Thread(rt);
//开启线程
t1.start();
t2.start();
}
}

3.TCP
A:最基本的TCP协议发送和接收数据
客户端:
public static ClientDemo{
public static void main(String[] args) throws IOException{
//创建客户端Socket对象
Socket s = new Socket("192.168.1.124",8888);
//创建输出流,写数据
OutputStream os = s.getOutputStream();
os.write("hello,TCP,我来了!".getBytes());
//释放资源
s.close();
}
}

服务器端:
public class ServerDemo{
public static void main(String[] args) throws IOException{
//创建服务器端Socket对象
ServerSocket ss = new ServerSocket(8888);
//监听客户端连接,返回一个Socket对象
Socket s = ss.accept();
//创建输入流,读取数据并显示在控制台
InputStream is = s.getInputStream();

byte[] bys = new byte[1024];
int len = is.read(bys);
String str = new String(bys,0,len);
String IP = s.getInetAddress().getHostAddress();
System.out.println(IP + " : " + str);
//释放资源
s.close();
}
}

B:服务器端给出反馈信息
客户端:
public class ClientDemo{
public static void main(String[] args) throws IOException {
//创建客户端Socket对象
Socket s = new Socket("192.168.1.124",10089);
//获取输出流对象,写出数据
OutputStream os = s.getOutputStream();
os.write("天气不错,适合睡觉!".getBytes());

//获取输入流对象,读入数据并显示
InputStream is = s.getInputStream();
byte[] bys = new byte[1024];
int len = is.read(bys);
String client = new String(bys,0,len);
System.out.println("client: "+client);
//关闭资源
s.close();
}
}

服务器端:
public class ServerDemo{
public static void main(String[] args) throws IOException {
//创建服务器端对象
ServerSocket ss = new ServerSocket(10089);
//监听客户端连接,返回Socket对象
Socket s = ss.accept();
//获取输入流对象,读入数据并显示
InputStream is = s.InputStream();
byte[] bys = new byte[1024];
len = is.read(bys);
String server = new String(bys,0,len);
System.out.println("server: " + server);

//获取输出流对象,写出反馈信息
OutputStream os = s.getOutputStream();
os.write("数据已收到!".getBytes());
//关闭资源
s.close();
}
}

C:客户端键盘录入,服务器控制台输出
客户端:
public class ClientDemo{
public static void main(String[] args) throws IOException {
//创建客户端Socket对象
Socket s = new Socket("192.168.1.124",9999);
//创建键盘录入对象
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//包装通道传输流为高效字符流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//写出数据
String line = null;
while((line = br.readLine()) != null){
if("886".equals(line)){
break;
}
bw.write(line);
bw.newLine();
bw.flush();
}
//关闭资源
s.close();
}
}

服务器端:
public class ServerDemo{
public static void main(String[] args) throws IOException {
//创建服务器端对象
ServerSocket ss = new ServerSocket(9999);
//监听客户端连接,返回Socket对象
Socket s = ss.accept();
//包装通道传输流数据为高效字符流
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//读出数据并显示
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
//关闭资源
s.close();
}
}

D:客户端键盘录入,服务器端写到文本文件
客户端:
public class ClientDemo{
public static void main(String[] args) throws IOException{
//创建客户端Socket对象
Socket s = new Socket("192.168.1.124",12345);
//封装键盘录入对象
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//把通道传输流封装为高效字符流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//写出数据
String line = null;
while((line = br.readLine()) != null){
if("over".equals(line)){
break;
}
bw.write(line);
bw.newLine();
bw.flush();
}
//关闭资源
s.close();
}
}

服务器端:
public class ServerDemo{
public static void main(String[] args) throws IOException{
//创建服务器端对象
ServerSocket ss = new ServerSocket(12345);
//监听客户端连接,返回Socket对象
Socket s = ss.accept();
//把通道传输流封装为高效字符流
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//封装文本文件写出数据
BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
String line = null;
while((line = br.readLine()) != null){
bw.write(line);
bw.newLine();
bw.flush();
}
//关闭资源
bw.close();
s.close();
}
}

E:客户端读取文本文件,服务器端控制台输出
客户端:
public class ClientDemo{
public static void main(String[] args) throws IOException{
//创建客户端Socket对象
Socket s = new Socket("192.168.1.124",10022);
//封装文本文件
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
//把通道传输流封装为高效字符流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//写出数据
String line = null;
while((line = br.readLine()) != null){
bw.write(line);
bw.newLine();
bw.flush();
}
//关闭资源
s.close();
br.close();
}
}

服务器端:
public class ServerDemo{
public static void main(String[] args){
//创建服务器端对象
ServerSocket ss = new ServerSocket(10022);
//监听客户端连接,返回Socket对象
Socket s = ss.accept();
//把通道传输流封装为高效字符流
BuferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream));
//读入数据并显示
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
//关闭资源
s.close();
}
}

F:客户端读取文本文件,服务器端写到文本文件,服务器端给出反馈。
客户端:
public calss ClientDemo{
public static void main(String[] args) throws IOException{
//创建客户端Socket对象
Socket s = new Socket("192.168.1.124",10088);
//封装文本文件对象
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
//把通道传输里封装为高效字符流
BufferedWriter bw = new BufferedWriter(new OutputStream(s.getOutputStream()));
//写出数据
String line = null;
while((line = br.readLine()) != null){
bw.write(line);
bw.newLine();
bw.flush();
}
//结束标记
s.shutdownOutput();
//接收反馈信息并显示
BufferedReader brClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = brClient.readLine();
System.out.println(str);
//关闭资源
s.close();
br.close();
}
}

服务器端:
public class ServerDemo{
public static void main(String[] args){
//创建服务器端对象
ServerSocket ss = new ServerSocket(10088);
//监听客户端连接,返回Socket对象
Socket s = ss.accept()
//封装文本文件
BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
//把通道传输流封装为高效字符流
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//写出文本
String line = null;
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();
//关闭资源
s.close();
bw.close();
}
}

G:上传图片到服务器,服务器端给出反馈信息
客户端:
public class ClientDemo{
public static void main(String[] args) throws IOException {
//创建客户端Socket对象
Socket s = new Socket("192.168.1.124",6666);
//封装图片对象
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("aa.jpg"));
//把通道传输流封装为高效字节流
BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
//写出数据
byte[] bys = new byte[1024];
int len = 0;
while((len = bis.read()) != -1){
bos.write(bys,o.len);
bos.flush();
}
//结束标记
s.shutdownOutput();
//接收反馈并显示
InputStream is = s.getInputStream();
byte[] bys2 = new byte[1024];
int len2 = is.read(bys2);
String client = new String(bys2,0,len2);
System.out.println(client);
//关闭资源
bis.close();
s.close();
}
}

服务器端:
public class ServerDemo{
public static void main(String[] args) throws IOException{
//创建服务器端对象
ServerSocket ss = new ServerSocket(6666);
//监听客户端,返回Socket对象
Socket s = ss.accept();
//封装图片对象
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("aa.jpg"));
//把通道传输流封装为高效字节流
BufferedInputStream bis = new BufferedInputStream(s.getInputStream);
//读出数据
byte[] bys = new byte{1024];
int len = 0;
while((len = bis.read(bys)) != -1){
bos.write(bys,0.len);
bos.flush();
}
//给出反馈
OutputStream os = s.getOutputStream();
os.write("图片上传完毕!".getBytes());
//关闭资源
bos.close();
s.close();
}
}

H:上传图片的多线程改进
客户端:
无变化
服务器端:
public class ServerThread implements Runnable{
private Socket s;
public ServerThread(Socket s){
this.s = s;
}

public void run(){
try{
//封装通道传输流为高效字节流
BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
//封装文件对象
String newName = System.currentTimeMillis() + ".jpg";
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newName));
//读入数据
byte[] bys = new byte[1024];
int len = 0;
while((len = bis.read(bys)) != -1){
bos.write(bys,0,len);
bos.flush();
}
//给出反馈
OutputStream os = s.getOutputStream();
os.write("图片上传完毕!".getBytes());
//关闭资源
bos.close();
r.close();
}catch(IOException e){
e.printStackTrace();
}
}
}

public class ServerDemo{
public static void main(String[] args) throws IOException {
//创建服务器端对象
ServerSocket ss = new ServerSocket(12345);

while(true){
Socket s = ss.accept();
new Thread(new ServerThread(s)).start();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: