关键字:TCP;URL
三、网络编程(TCP-客户端并发登录)
五、网络编程(浏览器客户端-Tomcat服务端)
六、网络编程(自定义浏览器-Tomcat服务端)
浏览器请求
GET / HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: zh-CN
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.
0)
Accept-Encoding: gzip, deflate
Host: 127.0.0.1:11000
Connection: Keep-Alive
服务器响应
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Accept-Ranges: bytes
ETag: W/"9951-1329416695650"
Last-Modified: Thu, 16 Feb 2012 18:24:55 GMT
Content-Type: text/html
Content-Length: 9951
Date: Fri, 17 Feb 2012 11:23:29 GMT
七、网络编程(URL-URLConnection)
socket文档
十、网络编程(域名解析)
一、网络编程(TCP-上传图片)
import java.io.*;
import java.net.*;
/*
需求:上传图片。
*/
/*
客户端:
1、服务端点。
2、读取客户端已有的图片数据
3、通过Socket输出流,将数据发给服务端
4、读取服务端反馈的信息
5、关闭
*/
class PicClient{
public static void main(String[] args) throws Exception{
Socket s = new Socket("127.0.0.1",10007);
FileInputStream fis = new FileInputStream("C:\\a.bmp");
OutputStream out = s.getOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while((len=fis.read(buf))!=-1){
out.write(buf,0,len);
}
s.shutdownOutput();
InputStream in = s.getInputStream();
byte[] bufIn = new byte[1024];
int num = in.read(bufIn);
System.out.println(new String(bufIn,0,num));
fis.close();
s.close();
}
}
/*
服务端:
*/
class PicServer{
public static void main(String[] args) throws Exception{
ServerSocket ss = new ServerSocket(10007);
Socket s = ss.accept();
InputStream in = s.getInputStream();
FileOutputStream fos = new FileOutputStream("server.bmp");
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();
ss.close();
}
}
二、网络编程(TCP-客户端并发上传图片)
import java.io.*;
import java.net.*;
/*
需求:上传图片。
*/
/*
客户端:
1、服务端点。
2、读取客户端已有的图片数据
3、通过Socket输出流,将数据发给服务端
4、读取服务端反馈的信息
5、关闭
*/
class PicClient{
public static void main(String[] args) throws Exception{
if(args.length!=1){
System.out.println("请选择一个jpg格式的图片");
return;
}
File file = new File(args[0]);
if(!(file.exists()&&file.isFile())){
System.out.println("该文件有问题,要么不存在,要不不是文件");
return;
}
if(!file.getName().endsWith(".jpg")){
System.out.println("图片格式错误,请重新选择jpg");
return;
}
if(file.length()>1024*1024){
System.out.println("图片过大");
return;
}
Socket s = new Socket("127.0.0.1",10007);
FileInputStream fis = new FileInputStream(file);
OutputStream out = s.getOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while((len=fis.read(buf))!=-1){
out.write(buf,0,len);
}
s.shutdownOutput();
InputStream in = s.getInputStream();
byte[] bufIn = new byte[1024];
int num = in.read(bufIn);
System.out.println(new String(bufIn,0,num));
fis.close();
s.close();
}
}
/*
服务端:
这个服务端有个局限性。当a客户端连接上后,被服务端获取到,服务段执行具体流程,
、b客户端只有等待,
因为服务端 还没有处理完a客户端的请求,还有循环回来执行下次accept方法,所以
暂时获取不到b客户端对象
那么为了可以让多个客户端同时并发访问服务端
那么服务端最好是将每个客户端封装到一个单独的线程中,这样,就可以同时处理多个客户端请求
如何定义线程呢?
只要明确了每一个客户端要在服务端执行的代码即可,将该代码存入到run方法中
*/
class PicThread implements Runnable{
private Socket s;
PicThread(Socket s){
this.s = s;
}
public void run(){
String ip = s.getInetAddress().getHostAddress();
try{
int count = 1;
System.out.println(ip+".....conneted");
InputStream in = s.getInputStream();
File file = new File(ip+".jpg");
while(file.exists()){
file = new File(ip+"("+(count++)+").jpg");
}
FileOutputStream fos = new FileOutputStream(file);
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());
System.out.println(ip+".....success");
fos.close();
s.close();
}catch(Exception e){
throw new RuntimeException("上传失败");
}
}
}
public class PicServer{
public static void main(String[] args) throws Exception{
ServerSocket ss = new ServerSocket(10007);
while(true){
Socket s = ss.accept();
new Thread(new PicThread(s)).start();
}
//ss.close();
}
}
三、网络编程(TCP-客户端并发登录)
/*
客户端通过键盘录入用户名
服务端对这个用户名进行校验
如果该用户存在,在服务端显示xxx,已登录
并在客户端显示,xxx,欢迎光临
如果该用户不存在,在服务器端显示xxx,尝试登陆
并在客户端显示 xxx,该用户不存在
最多就登陆三次
*/
import java.io.*;
import java.net.*;
/*
需求:上传图片。
*/
/*
客户端:
*/
class LoginClient{
public static void main(String[] args) throws Exception{
Socket s = new Socket("127.0.0.1",10008);
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
for(int x= 0;x<3;x++){
String line = bufr.readLine();
if(line == null||line.length()==0)
break;
out.println(line);
String info = bufIn.readLine();
System.out.println("info:"+info);
if(info.contains("欢迎"))
break;
}
bufr.close();
s.close();
}
}
/*
服务端:
*/
class UserThread implements Runnable{
private Socket s;
UserThread(Socket s){
this.s = s;
}
public void run(){
String ip = s.getInetAddress().getHostAddress();
try{
for(int x=0;x<3;x++){
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String name = bufIn.readLine();
if(name==null)
break;
BufferedReader bufr = new BufferedReader(new FileReader("user.txt"));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
String line = null;
boolean flag = false;
while((line = bufr.readLine())!=null){
if(line == null)
break;
if(line.equals(name)){
flag =true;
break;
}
}
if(flag){
System.out.println(name+"已登录");
out.println(name+",欢迎观临");
break;
}else{
System.out.println(name+"尝试登陆");
out.println(name+",用户名不存在");
}
}
s.close();
}catch(Exception e){
throw new RuntimeException(ip+"校验失败");
}
}
}
class LoginServer{
public static void main(String[] args) throws Exception{
ServerSocket ss = new ServerSocket(10008);
while(true){
Socket s = ss.accept();
new Thread(new UserThread(s)).start();
}
}
}
四、网络编程(浏览器客户端-自定义服务端)
/*
演示客户端和服务端
1、
客户端:浏览器
服务端:自定义
*/
import java.io.*;
import java.net.*;
public class Demo{
public static void main(String[] args) throws Exception{
ServerSocket ss = new ServerSocket(11000);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip);
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
out.println("<font color='red' size='7'>"+ip+"客户端你好</font>");
s.close();
ss.close();
}
}
五、网络编程(浏览器客户端-Tomcat服务端)
/*
演示客户端和服务端
1、
客户端:浏览器
服务端:自定义
2、
客户端:浏览器
服务端:Tomcat 服务器
*/
六、网络编程(自定义浏览器-Tomcat服务端)
浏览器请求
GET / HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: zh-CN
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.
0)
Accept-Encoding: gzip, deflate
Host: 127.0.0.1:11000
Connection: Keep-Alive
服务器响应
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Accept-Ranges: bytes
ETag: W/"9951-1329416695650"
Last-Modified: Thu, 16 Feb 2012 18:24:55 GMT
Content-Type: text/html
Content-Length: 9951
Date: Fri, 17 Feb 2012 11:23:29 GMT
/*
演示客户端和服务端
1、
客户端:浏览器
服务端:自定义
2、
客户端:浏览器
服务端:Tomcat 服务器
3、
客户端:自定义
服务端:Tomcat服务器
*/
import java.io.*;
import java.net.*;
public class Demo{
public static void main(String[] args) throws Exception{
Socket s = new Socket("127.0.0.1",8080);
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
out.println("GET /myweb/index.html HTTP/1.1");
out.println("Accept: */*");
out.println("Accept-Language: zh-cn");
out.println("Host: 127.0.0.1:11000");
out.println("Connection: Keep-Alive");
out.println();
out.println();
BufferedReader bufr = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line =null;
while((line=bufr.readLine())!=null){
System.out.println(line);
}
s.close();
}
}
七、网络编程(URL-URLConnection)
/*
URL(String spec) 根据 String 表示形式创建 URL 对象。
URL(String protocol, String host, int port, String file) 根据指定 protocol、host、port 号和 file 创建 URL 对象。
Object getContent() 获取此 URL 的内容。
Object getContent(Class[] classes) 获取此 URL 的内容。
int getDefaultPort() 获取与此 URL 关联协议的默认端口号。
String getFile() 获取此 URL 的文件名。
String getHost() 获取此 URL 的主机名(如果适用)。
String getPath() 获取此 URL 的路径部分。
int getPort() 获取此 URL 的端口号。
String getProtocol() 获取此 URL 的协议名称。
String getQuery() 获取此 URL 的查询部分。
*/
import java.io.*;
import java.net.*;
public class Demo{
public static void main(String[] args) throws Exception{
URL url = new URL("http://127.0.0.1:8080/myweb/index.html?a=1&b=2");
sop("getDefaultPort() :"+url.getDefaultPort());//getDefaultPort() :80
sop("getFile() :"+url.getFile());//getFile() :/myweb/index.html
sop("getHost() :"+url.getHost());//getHost() :127.0.0.1
sop("getPath() :"+url.getPath());//getPath() :/myweb/index.html
sop("getPort() :"+url.getPort());//getPort() :8080,如果没有填,则获取的时候,会返回-1,可以判断是否为-1,给他一个端口号
sop("getProtocol() :"+url.getProtocol());//getProtocol() :http
sop("getQuery() :"+url.getQuery());//getQuery() :a=1&b=2
}
public static void sop(Object obj){
System.out.println(obj);
}
}
/*
URLConnection openConnection() 返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。
*/
import java.io.*;
import java.net.*;
public class Demo{
public static void main(String[] args) throws Exception{
URL url = new URL("http://127.0.0.1:8080/myweb/index.html");
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in,"utf-8"));
String line = null;
while((line = br.readLine())!=null){
sop(line);
}
}
public static void sop(Object obj){
System.out.println(obj);
}
}
九、网络编程(小知识点)
socket文档
十、网络编程(域名解析)