查看本机IP地址,在控制台输入:ipconfig
检查网络是否连通,在控制台输入:ping 空格 IP地址
localhost代表本机地址
客户端
Socket:实现客户端套接字,套接字是两台机器间通信的端点
Socket(String host,int port);//String host:IP地址//int port:服务器的端口号
getOutputStream();//返回此套接字的输出流
getInputStream ();//返回此套接字的输入流
close();//关闭此套接字
shutdownOutput;//禁用此套接字的输出流
服务器端
ServerSocket:服务器端
ServerSocket(int port);
accept();//监听并接受到此套接字的连接
客户端
import java.io.*;
import java.net.Socket;
public class Kehuduan {
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("D:\\1.jpg");
Socket socket=new Socket("192.168.10.30",8888);
OutputStream os=socket.getOutputStream();
int len=0;
byte[] bytes=new byte[1024];
while((len=fis.read(bytes))!=-1){
os.write(bytes,0,len);
}
socket.shutdownOutput();
InputStream is = socket.getInputStream();
while((len=is.read(bytes))!=-1){
System.out.println(new String(bytes,0,len));
}
fis.close();
socket.close();
}
}
服务器
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Fuwuqi {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket=new ServerSocket(8888);
Socket s = serverSocket.accept();
InputStream is = s.getInputStream();
File file=new File("D:\\1.jpg");
if(!file.exists()){
file.mkdir();
}
FileOutputStream fos=new FileOutputStream("E:\\"+System.currentTimeMillis());
int len=0;
byte[] bytes=new byte[1024];
while((len=is.read(bytes))!=-1){
fos.write(bytes);
}
OutputStream os = s.getOutputStream();
os.write("上传成功".getBytes());
fos.close();
serverSocket.close();
}
}
BS是浏览器和服务器结构,一般指B/S结构
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Demo04 {//这是模拟服务器
public static void main(String[] args) throws IOException {
ServerSocket serverSocket=new ServerSocket(8888);
while (true){//读取图片,每读取一次图片,建立一个新线程
Socket socket=serverSocket.accept();
new Thread(new Runnable() {
@Override
public void run() {
try{
InputStream is=socket.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String s = br.readLine();
Systenm.out.println(line);//输出一下请求的路径
String[] arr=s.split(" ");
String htmlpath=arr[1].substring(1);
FileInputStream fis=new FileInputStream(htmlpath);
OutputStream os=socket.getOutputStream();
//使用HTTP协议响应头,固定格式
os.write("HTTP/1.1 200 OK\r\n".getBytes());
os.write("Content-Type:text/html\r\n".getBytes());
//必须写入空行,否则浏览器不解析
os.write("\r\n".getBytes()) ;
int len=0;
byte[] bytes=new byte[1024];
while ((len=fis.read(bytes))!=-1){
System.out.println(new String(bytes,0,len));
}
fis.close();
os.close();
socket.close();
}catch (IOException e){
System.out.println(e);
}
}
}).start();
}
}
}
前言: 今天这篇文章给大家介绍关于ajax的content-download时间过慢问题的解决与...
OCR光学字符识别OPTICAL CHARACTER RECOGNITION作为计算机视觉领域的经典问题之...
来自:机器之心 最近在 GitHub 上最火的项目是一个对视力友好的十六进制编辑器,...
微软确认, 将会在Win10 Build 19043.899(21H1)更新中,彻底从系统中删除经典...
有时候我么您需要获取网址,端口、路径文件名、参数等,这里就为大家分享一下这...
为什么我们需要它 不得不说,在知道这个命令的时,以及之后的使用中,我都超级热...
如往常一样, 客户发给我一个xml文件, 用来更新数码课堂日程安排——是一个js读...
JSP开发中Apache-HTTPClient 用户验证的实例详解 前言: 在微服务框架之外的系统...
HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS、GET、HEAD、POST、PUT、DELETE、...
本文实例为大家分享了JSP+Servlet实现文件上传到服务器功能的具体代码,供大家参...