在简单的网络编程中,需要服务器与客户端的连接
一.TCP的简单使用(可信但效率低)
①自定义客户端与服务器,进行图片的复制
客户端
public void client()
{
OutputStream os = null;
FileInputStream file1 = null;
InputStream is = null;
ByteArrayOutputStream bo = null;
try {
//ip地址
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
os = socket.getOutputStream();
file1 = new FileInputStream(new File("OIP1-C.jpg"));
//读取图片的信息
byte[] buffer = new byte[100];
int lent;
while ((lent = file1.read(buffer)) != -1)
{
os.write(buffer,0,lent);
}
//告诉服务器传输的数据结束
socket.shutdownInput();
//读入来自服务器的数据
is = socket.getInputStream();
bo = new ByteArrayOutputStream();
byte[] arr = new byte[100];
int len;
while ((len = is.read(arr) ) != -1)
{
bo.write(arr,0,len);
}
System.out.println(bo.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
//流的关闭
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (file1 != null) {
try {
file1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bo != null) {
try {
bo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
服务器
public void server()
{
ServerSocket ss = null;
Socket accept = null;
InputStream is = null;
FileOutputStream file2 = null;
OutputStream outputStream = null;
try {
//自己的端口值
ss = new ServerSocket(9090);
accept = ss.accept();
is = accept.getInputStream();
file2 = new FileOutputStream("`OIP12-C.jpg");
//复制图片的操作
byte[] buffer = new byte[100];
int lent;
while ((lent = is.read()) != -1)
{
file2.write(buffer,0,lent);
}
//传给客户端的信息
outputStream = accept.getOutputStream();
outputStream.write("已收到".getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
} finally {
while (ss != null) {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
while (accept != null) {
try {
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
}
while (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
while (file2 != null) {
try {
file2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
while (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
②使用浏览器与Tomcat服务器的连接
首先需要配置tomcat,在控制台输入catalina run后在浏览器输入 http://localhost:8080
可随意在一个地方创建一个txt,然后在浏览器输入http://localhost:8080/1/hello.txt(举例)
便可在网页看到自己在文本输入的内容了
2.UDP(不可信但效率高)
发送
public void sender()
{
DatagramSocket socket = null;
try {
socket = new DatagramSocket();
String arr = "我传输了信息过来";
byte[] data = arr.getBytes(StandardCharsets.UTF_8);
InetAddress ient = InetAddress.getLocalHost();
DatagramPacket packet = new DatagramPacket(data,0,data.length,ient,8080);
//将信息打包并发送出去
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(socket != null)
socket.close();
}
}
接收
public void reader() throws Exception
{
//服务器的端口
DatagramSocket data = new DatagramSocket(8080);
byte[] bytes = new byte[100];
DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length);
data.receive(packet);
System.out.println(new String(packet.getData(),0,packet.getLength()));
}
③URL(较为常用)实现tomcat服务端数据下载
public static void main(String[] args) {
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
try {
URL url = new URL("http://localhost:8080/1/bea.jpg");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
inputStream = urlConnection.getInputStream();
fileOutputStream = new FileOutputStream("bea1,jpg");
byte[] bytes = new byte[1000];
int len;
while ((len = inputStream.read()) != -1)
{
fileOutputStream.write(bytes,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (urlConnection != null)
urlConnection.disconnect();
}
}