初学网络编程
使用TCP协议socket完成作业
cilent端
//(1) 使用字符流的方式,编写一个客户端程序和服务器端程序,
//(2) 客户端发送"name",服务器端接收到后,返回"我是 nova", nova 是你自己的名字
//(3) 客户端发送"hobby",服务器端接收到后,返回"编写 java 程序"
//(4) 不是这两个问题,回复"你说啥呢”
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class homework1client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getByName("192.168.31.88"), 9998);
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream));
// bw.write("name");
// bw.write("hobby");
bw.write("finial");
bw.newLine();
bw.flush();
InputStream inputStream = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String line = br.readLine();
System.out.println(line);
br.close();
bw.close();
socket.close();
System.out.println("客户端运行完毕");
}
}
server端
package src.chapter14.api.homework; /* viktor_qiu compile */
//(1) 使用字符流的方式,编写一个客户端程序和服务器端程序,
//(2) 客户端发送"name",服务器端接收到后,返回"我是 nova", nova 是你自己的名字
//(3) 客户端发送"hobby",服务器端接收到后,返回"编写 java 程序"
//(4) 不是这两个问题,回复"你说啥呢”
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class homework1server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9998);
System.out.println("正在等待链接...");
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String s= br.readLine();
System.out.println(s);
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream));
if(s.equals("name")){
bw.write("我是nove");
}
else if(s.equals("hobby")){
bw.write("编写java程序");
}
else{
bw.write("你说啥呢");
}
bw.newLine();
bw.flush();
bw.close();
br.close();
socket.close();
serverSocket.close();
System.out.println("运行完毕server端");
}
}
使用DSP协议完成作业
Receiver端
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* (1) 编写一个接收端 A,和一个发送端 B,使用 UDP 协议完成
* (2) 接收端在 8888 端口等待接收数据 (receive)
* (3) 发送端向接收端发送数据"四大名著是哪些”
* (4) 接收端接收到发送端发送的问题后,返回?"四大名著是<<红楼梦>>…",否则返回 what?
* (5) 接收端和发送端程序退出
*/
public class homework2Receiver {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket(9988);
byte[] buf =new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
System.out.println("等待接收数据");
ds.receive(packet);
int length = packet.getLength();
byte[] data = packet.getData();
String s = new String(data, 0, length);
System.out.println(s);
byte[] bytes = "四大名著是《红楼梦》...".getBytes();
DatagramPacket packet1 = new DatagramPacket(bytes, bytes.length, InetAddress.getByName("192.168.31.88"), 9998);
ds.send(packet1);
ds.close();
System.out.println("接收端完毕");
}
}
sender端
package src.chapter14.api.homework; /* viktor_qiu compile */
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* (1) 编写一个接收端 A,和一个发送端 B,使用 UDP 协议完成
* (2) 接收端在 8888 端口等待接收数据 (receive)
* (3) 发送端向接收端发送数据"四大名著是哪些”
* (4) 接收端接收到发送端发送的问题后,返回?"四大名著是<<红楼梦>>…",否则返回 what?
* (5) 接收端和发送端程序退出
*/
public class homework2sender {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket(9998);
byte[] bytes = "四大名著有哪些".getBytes();
DatagramPacket packet = new DatagramPacket(bytes, bytes.length, InetAddress.getByName("192.168.31.88"), 9988);
ds.send(packet);
byte[] buff = new byte[1024];
DatagramPacket packet1 = new DatagramPacket(buff, buff.length);
System.out.println("准备接收数据");
ds.receive(packet1);
int length = packet1.getLength();
byte[] data = packet1.getData();
System.out.println(new String(data,0,length));
ds.close();
System.out.println("发送端完毕");
}
}
完成二进制文件在网络上的传输TCP协议
server端
package src.chapter14.api.Upload; /* viktor_qiu compile */
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class UploadServer {
public static void main(String[] args) throws Exception {
//服务端代码在本机监听8888端口
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("服务端在8888监听");
Socket socket = serverSocket.accept();
//读取客户端发送的数据
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
byte[] bytes = StreamUtil.streamToBYteArray(bis);
//将得到的bytes,写入指定的路径,就得到一个文件
String dest = "src\\琪琪.png";
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));
bos.write(bytes);
bos.close();
//向客户端回复收到图片
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write("小子,我拿到啦\n我谢谢你");
// bw.newLine();
bw.flush();
socket.shutdownOutput();
//关闭流
bw.close();
bis.close();
socket.close();
serverSocket.close();
}
}
cilent端
package src.chapter14.api.Upload; /* viktor_qiu compile */
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class UploadClient {
public static void main(String[] args) throws Exception {
Socket socket = new Socket(InetAddress.getLocalHost(), 8888);
//创建读取磁盘文件的输入流
String filePath = "C:\\Users\\98414\\Pictures\\七七瑶瑶.png";
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath));
byte[] bytes = StreamUtil.streamToBYteArray(bis);
//通过socket获得到输出流,将bytes数据发送给服务端
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
bos.write(bytes); //将文件的对应的字节数据的内容,写入通道
bis.close();
socket.shutdownOutput();//设置写入数据结束标记
//得到服务端的回复信息
InputStream inputStream = socket.getInputStream();
String s=StreamUtil.streamToString(inputStream);
System.out.println(s);
//关闭流
bos.close();
socket.close();
}
}
StreamUtil工具类
package src.chapter14.api.Upload; /* viktor_qiu compile */
import java.io.*;
public class StreamUtil {
public static byte[] streamToBYteArray (InputStream is) throws Exception{
ByteArrayOutputStream bos = new ByteArrayOutputStream();//创建一个输入流对象
byte[] b = new byte[1024];
int len;
while((len=is.read(b))!=-1){
bos.write(b,0,len); //把读取到数据,写入到bos
}
byte[] array = bos.toByteArray();//文件所有的内容转换成bos
bos.close();
return array;
}
public static String streamToString(InputStream ins) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(ins));
StringBuilder builder = new StringBuilder();
String line;
while((line = bufferedReader.readLine())!=null){
builder.append(line+"\r\n");
}
return builder.toString();
}
}
结语
TCP相对来说DSP更加可靠
2023/5/10 第一次接触网络编程 成功应用