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.csdn.net;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class UploadServer {
/**
* @param args
* @throws IOException
*/
publicstatic void main(String[] args) throws IOException {
ServerSocketss=new ServerSocket(9009);
Sockets=ss.accept();
Stringip=s.getInetAddress().getHostAddress();
System.out.println(ip+"……connection");
BufferedReaderbrin =new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriterpw=new PrintWriter(newFileWriter("C:/Users/Yule/Desktop/server.txt"),true);
Stringline=null;
while((line=brin.readLine())!=null){
pw.println(line);
}
PrintWriterpwout=new PrintWriter(s.getOutputStream(),true);
pwout.println("上传成功!!");
pw.close();
s.close();
ss.close();
}
}
客户端
package com.csdn.net;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class UploadClient {
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
publicstatic void main(String[] args) throws Exception {
Sockets=new Socket("192.168.49.186",9009);
BufferedReaderbr=new BufferedReader(newFileReader("C:/Users/Yule/Desktop/server.txt"));
PrintWriterpwout=new PrintWriter(s.getOutputStream(),true);
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.csdn.net;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ImgServer {
/**
* @param args
*/
publicstatic void main(String[] args) throws IOException {
ServerSocketss=new ServerSocket(9009);
while(true){
Sockets=ss.accept();
newThread(new ImgThread(s)).start();
}
//ss.close();
}
}
class ImgThread implements Runnable{
privateSocket s;
publicImgThread(Socket s){
this.s=s;
}
@Override
publicvoid run() {
intcount=1;
try{
Stringip=s.getInetAddress().getHostAddress();
System.out.println(ip+"……connection");
InputStreamin=s.getInputStream();
Filedir=new File("C:/Users/Yule/Desktop");
Filef=new File(dir,ip+"("+count+").jpg");
while(f.exists()){
f=newFile(dir,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(Exceptione){
e.printStackTrace();
}
}
}
客户端
package com.csdn.net;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
public class ImgClient {
/**
* @param args
*/
publicstatic void main(String[] args) throws Exception {
// if(args.length==0){
// System.out.println("请指定一个jpg文件");
// return;
// }
// Filef=new File(args[0]);
Sockets=new Socket("192.168.49.205",9009);
FileInputStreamfis=new FileInputStream("F:\\Picture\\壁纸\\图片\\1(1).jpg");
OutputStreamout=s.getOutputStream();
byte[]buf=new byte[1024];
int len=0;
while((len=fis.read(buf))!=-1){
out.write(buf,0, len);
}
s.shutdownOutput();
InputStreamin=s.getInputStream();
byte[]b=new byte[1024];
intnum=in.read(b);
System.out.println(newString(b,0,num));
fis.close();
s.close();
}
}