//单纯的客户端服务端有个局限性,A客户端连接上以后,未处理完之前,B客户端连接只有等待
//所以服务端最好将每个客户端封装到一个线程中
class PicThread implements Runnable
{
private Socket s;
public PicThread(Socket socket) {
// TODO Auto-generated constructor stub
this.s = socket;
}
public void run() {
int count =1;
String ip = s.getInetAddress().getHostAddress();
try {
{
private Socket s;
public PicThread(Socket socket) {
// TODO Auto-generated constructor stub
this.s = socket;
}
public void run() {
int count =1;
String ip = s.getInetAddress().getHostAddress();
try {
System.out.println(ip+"connect");
InputStream in = s.getInputStream();
FileOutputStream fos = new FileOutputStream("");
byte[] buf = new byte[1024];
int len = 0;
while((len=in.read(buf))!=-1)
{
fos.write(buf,0,len);
}
OutputStream out = s.getOutputStream();
out.write("上传成功".getBytes());
fos.close();
s.close();
} catch (Exception e) {
// TODO: handle exception
throw new RuntimeException("上传失败");
}
}
}
InputStream in = s.getInputStream();
FileOutputStream fos = new FileOutputStream("");
byte[] buf = new byte[1024];
int len = 0;
while((len=in.read(buf))!=-1)
{
fos.write(buf,0,len);
}
OutputStream out = s.getOutputStream();
out.write("上传成功".getBytes());
fos.close();
s.close();
} catch (Exception e) {
// TODO: handle exception
throw new RuntimeException("上传失败");
}
}
}
//windows上远程登陆命令 telnet IP Port
客户端服务端可进行:
1
客户端:浏览器(telent)
服务端:自定义
2
客户端:浏览器
服务端:tomcat服务器
URLConnection
//Sorket是在传输层进行,而URLConnection可以通过URL对象的方法获取,通过InputStream方法,在应用层对其进行操作,
//自带拆http协议头的BGM
//InetSorketAddress包括IP地址和端口
域名解析
//先走本地在联网搜索,联网搜索时若搜索的为主机名,则去默认DNS中解析出IP,再去搜索