1.客户端上传一个文件通过TCP传送给服务端,服务端发送一个反馈信息给客户端确认接收
1: import java.io.BufferedReader;
2: import java.io.DataInputStream;
3: import java.io.DataOutputStream;
4: import java.io.FileReader;
5: import java.io.FileWriter;
6: import java.io.InputStreamReader;
7: import java.io.PrintWriter;
8: import java.net.ServerSocket;
9: import java.net.Socket;
10:
11: //写时间戳很麻烦,定义标记的简便方式shutdownInput、shutdownOutput,
12: //相当于为流加入结束标记。
13: public class TextClient {
14:
15: public static void main(String[] args)throws Exception
16: {
17: //建立指定主机和端口的socket服务
18: Socket s =new Socket("192.168.1.154",10056);
19: //建立文件读取流对象关联要读取的文件,并将流存入缓冲区
20: BufferedReader bufr=
21: new BufferedReader(new FileReader("1.txt"));
22: //定义打印流对象,把流内容打印到socket服务的字节输出流中
23: PrintWriter out =new PrintWriter(s.getOutputStream(),true);
24: /*
25: 定义一个时间戳作为结束标记
26: DataOutputStream dos=new DataOutputStream(s.getOutputStream());
27: long time=System.currentTimeMillis();
28: dos.writeLong(time);
29: */
30: String line=null;
31: while((line=bufr.readLine())!=null)
32: {
33: //打印流对象将文件的字节内容不断的输出到socket服务的字节输出流中
34: out.println(line);
35: }
36:
37: //dos.writeLong(time);//写一个时间戳
38: //客户端和服务端的交互,也要获得服务端的反馈信息
39: //getInputStream():返回此套接字的输入流。
40: BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
41:
42: String str=bufIn.readLine();
43: //看一下服务端都说了什么
44: System.out.println(str);
45: bufr.close();
46: s.close();
47:
48: }
49: }
50: //服务端代码和客户端是对应的,相反的一个过程
51: class TextServer
52: {
53: public static void main(String[] args) throws Exception
54: {
55: /*建立服务端ServerSocket监听一个端口
56: * 此类实现服务器套接字。
57: * 服务器套接字等待请求通过网络传入。它基于该请求执行某些操作,然后可能向请求者返回结果。
58: **/
59: ServerSocket ss =new ServerSocket(10056);
60: //accept(), 侦听并接受到此套接字的连接。
61: Socket s=ss.accept();
62: //DataInputStream dis=new DataInputStream(s.getInputStream());
63: //long l=dis.readLong();//读时间戳
64: BufferedReader bufIn=
65: new BufferedReader(new InputStreamReader(s.getInputStream()));
66:
67: PrintWriter out =new PrintWriter(new FileWriter("server.txt"),true);
68: String line=null;
69: while((line=bufIn.readLine())!=null)
70: {
71: if("over".equals(line));//读取到结束标记
72: out.println(line);
73: }
74: PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
75: pw.println("上传成功");
76: out.close();
77: s.close();
78: ss.close();
79: }
80: }
2.
/**
* 利用多线程解决多客户并发连接服务端的问题
* 复制图片过程:
* 客户端:服务端点、读取一个图片文件、通过
* socket输出流将数据发给服务端
* 读取服务端的反馈信息
* 关闭
*/
1: import java.io.File;
2: import java.io.FileInputStream;
3: import java.io.FileOutputStream;
4: import java.io.InputStream;
5: import java.io.OutputStream;
6: import java.net.ServerSocket;
7: import java.net.Socket;
8:
9: class PicThread implements Runnable
10: {
11: private Socket s;
12: PicThread(Socket s)
13: {
14: this.s=s;
15: }
16: public void run()
17: {
18: int count=1;
19: String ip=s.getInetAddress().getHostAddress();
20: try {
21: InputStream in=s.getInputStream();
22:
23: //防止多客户上传来的图片发生覆盖
24: File file=new File(ip+"("+(count++)+")"+".pmp");
25: while(file.exists())
26: file=new File(ip+"("+(count++)+")"+".pmp");
27: FileOutputStream fos=new FileOutputStream(file);
28: byte[] buf=new byte[1024];
29: int len=0;
30: while((len=in.read(buf))!=-1)
31: {
32: fos.write(buf,0,len);
33: }
34: OutputStream out=s.getOutputStream();
35: out.write("上传成功".getBytes());
36: fos.close();
37: s.close();
38:
39: } catch (Exception e) {
40: throw new RuntimeException("失败了");
41: }
42: }
43: }
44: public class PicClient
45: {
46: public static void main(String[] args)throws Exception
47: {
48: if(args.length!=1)
49: {
50: System.out.println("请选择一个图片");
51: return;
52:
53: }
54: File file=new File(args[0]);
55: if(!(file.exists()&&file.isFile()))
56: {
57: System.out.println("图片有问题");
58: return;
59: }
60: if(!(file.getName().endsWith(".jpg")))
61: {
62: System.out.println("图片有问题");
63: return;
64: }
65: if(file.length()>1024*1024*5)
66: {
67: System.out.println("图片有问题");
68: return;
69: }
70: Socket s=new Socket("192.168.1.254",10006);
71: FileInputStream fis =new FileInputStream(file);
72: OutputStream out =s.getOutputStream();
73: byte[] buf=new byte[1024];
74: int len=0;
75: while((len=fis.read(buf))!=-1)
76: {
77: out.write(buf,0,len);
78: }
79: //告诉服务端数据已写完
80: s.shutdownOutput();
81: InputStream in=s.getInputStream();
82: byte[] bufIn=new byte[1024];
83: int num=in.read(bufIn);
84: System.out.println(new String(bufIn,0,num));
85: fis.close();
86: s.close();
87: }
88:
89: }
90: /*服务器的读取图片代码要被多个客户端执行,所以要封装到线程中,否则一个
91: 客户连进来,另一个客户只能等待,因为没有处理完上一个客户时,下一个客户无法
92: 获取到accept方法
93: */
94: class PicServer//阻塞式方法,不会一直读
95: {
96: public static void main(String[] args)throws Exception
97: {
98: ServerSocket ss=new ServerSocket(10006);
99:
100: while(true)
101: {
102: Socket s=ss.accept();//拿到accept很重要
103: //进来一个客户就去拿自己的那份服务端线程,并开启线程 。
104: new Thread(new PicThread(s)).start();
105: }
106:
107:
108: }
109: }
3.服务端自定义,客户端浏览器
1: import java.io.PrintWriter;
2: import java.net.ServerSocket;
3: import java.net.Socket;
4: public class ServerDamo
5: {
6: public static void main(String[] args)throws Exception
7: {
8: ServerSocket ss=new ServerSocket(11001);
9: Socket s=ss.accept();
10: System.out.println(s.getInetAddress().getHostAddress());
11:
12: PrintWriter out=new PrintWriter(s.getOutputStream(),true);
13: out.println("呼叫客户端");
14: s.close();
15: ss.close();
16: }
4.//客户端自定义,服务端tomcat服务器
1: import java.io.BufferedReader;
2: import java.io.InputStreamReader;
3: import java.io.PrintWriter;
4: import java.net.Socket;
5:
6: public class ClientDamo
7: {
8:
9: public static void main(String[] args) throws Exception
10: {
11: //tomcate端口是8080
12: Socket s=new Socket("192.168.1.254",8080);
13: PrintWriter out=new PrintWriter(s.getOutputStream(),true);
14: out.println("");
15: out.println("");
16: out.println("");//往tomcat发特定格式的请求消息
17: BufferedReader bufr=new BufferedReader(new InputStreamReader(s.getInputStream()));
18: String line=null;
19: //读取tomcat发回来的应答消息
20: while((line=bufr.readLine())!=null)
21: {
22: System.out.println(line);
23: }
24: s.close();
25: }
26:
27: }
5.URLConnection:3G里常用的类,它作用于应用层,拆包自己的信息并解读
/*URL有很多方法,有几个很重要的获取方法如:表单提交
* 获取参数信息的时候
*/
1: import java.io.InputStream;
2: import java.net.URL;
3: import java.net.URLConnection;
4: public class URLConnectionDemo
5: {
6: public static void main(String[] args) throws Exception
7: {
8: URL url=new URL("http://127.0.0.1/myweb");
9: //返回一个urlconection连接对象,表示它到URL所引起的 远程对象的连接
10: //已连接上网址对应的主机,socket都不用写了,而且提供了很多方法
11: URLConnection conn=url.openConnection();//打开连接了
12:
13: InputStream in =conn.getInputStream();
14: byte[] buf=new byte[1024];
15: int len =in.read(buf);
16: System.out.println(new String(buf,0,len));
17: }
18:
19: }
1: import java.net.URL;
2:
3:
4: public class URLdemo
5: {
6: public static void main(String[] args)throws Exception
7: {
8: URL url=new URL("http://127.0.0.1/myweb/?name=haha;");
9: System.out.println(url.getQuery());//返回name=haha
10: }
11:
12: }