Java_乔晓松_TCP客户端想服务器端上传照片和文本文件
tcp协议:
1.面向连接的
2.通过三次握手完成连接
3.可靠地协议
4.相对于udp 稍慢
分为服务端:SeverSocket
和客户端:Socket
服务端步骤:
1.建立tcp Socket服务,必须要监听一个端口
ServerSocket ss=new ServerSocket(port);
2.通过accptet方法,返回一个socket对象,同过这个socket与客户端的socket建立连接。
Socket s= ss.accept();
3.通过socket获取输入输出流,进行信息通信
写:s.getOutputStream();
读:s.getInputStream()
4.关闭资源
s.close();
ss.close();
客户端
1.建立Socket客户端,ip代表要连接的目的地址和端口
Socket s=new Socket(ip,port)
2.通过socket获取输入输出流与服务端通信
写:s.getOutputStream();
读:s.getInputStream()
3.关闭资源。
s.close();
//向服务器端发送文本文件
package com.hbsi.net;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
* 向服务器端传送文本文件
*@author 乔晓松
*
*/
public class UploadClient {
/**
* @param args
*/
publicstatic void main(String[] args) throws Exception{
Sockets=new Socket("192.168.49.104",9009);
PrintWriterpwout=new PrintWriter(s.getOutputStream(),true);
BufferedReaderbr=new BufferedReader(new FileReader("E:\\Client.txt"));
Stringline=null;
while((line=br.readLine())!=null){
pwout.println(line);
}
s.shutdownOutput();
BufferedReaderbrin=new BufferedReader(new InputStreamReader(s.getInputStream()));
Stringstr=brin.readLine();
System.out.println(str);
br.close();
s.close();
}
}
package com.hbsi.net;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class UploadServer {
/**
* @param args
*/
publicstatic void main(String[] args) throws Exception{
ServerSocketss=new ServerSocket(9009);
Sockets=ss.accept();
InputStreamin=s.getInputStream();
BufferedReaderbrin=new BufferedReader(new InputStreamReader(in));
PrintWriterpw=new PrintWriter(new FileWriter("E:\\Server.txt"), true);
Stringline=null;
while((line=brin.readLine())!=null){
pw.println(line);
}
PrintWriterpwout=new PrintWriter(s.getOutputStream(), true);
pwout.println("Server:上传成功");
pw.close();
s.close();
ss.close();
}
}
//向服务器端上传图片
package com.hbsi.net;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* 客户端,向指定服务器端指定接口服务器端发送图片文件
* @author乔晓松
*
*/
public class JpgClient {
/**
* @param args
*/
public static void main(String[] args) {
if(args.length==0){
System.out.println("请输入jpg图片路径");
}
File f=new File(args[0]);
try {
Socket s=new Socket("192.168.49.104",9009);
OutputStream out=s.getOutputStream();
FileInputStream fis=new FileInputStream(f);
int len=0;
byte[] buf=newbyte[1024];
while((len=fis.read(buf))!=-1){
out.write(buf, 0, len);
}
s.shutdownOutput();
InputStream in=s.getInputStream();
byte[] b=newbyte[1024];
int i=in.read(b);
System.out.println(new String(b,0,i));
fis.close();
s.close();
} catch (UnknownHostException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
}
}
}
package com.hbsi.net;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 服务器端,接受9009端口的图片,使用了多线程
*@author 乔晓松
*
*/
public class JpgServer {
/**
* @param args
*/
publicstatic void main(String[] args) {
try{
ServerSocketss=new ServerSocket(9009);
while(true){
Sockets=ss.accept();
newThread(new ServerThread(s)).start();
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
class ServerThread implements Runnable{
Sockets;
publicServerThread(Socket s){
this.s=s;
}
@Override
publicvoid run() {
intcount=1;
try{
InputStreamin=s.getInputStream();
Stringip=s.getInetAddress().getHostAddress();
System.out.println(ip+"-----connection");
Filefi=new File("E:\\");
Filef=new File(fi,ip+"("+count+").jpg");
while(f.exists())
f=newFile(fi,ip+"("+(count++)+").jpg");
FileOutputStreamfos=new FileOutputStream(f);
byte[]buf=new byte[1024];
intlen=0;
while((len=in.read(buf))!=-1){
fos.write(buf,0, len);
}
OutputStreamout=s.getOutputStream();
out.write("上传成功了".getBytes());
fos.close();
s.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}