TCP协议(服务器端程先启动,等待客户端连接)

TCP协议是面向连接的通信协议,即在传输数据前先在发送端和接收端建立逻辑连接,然后再传输数据

保证传输数据的全性安,文件数据不易丢失

在JDK中提供了两个类用于实现TCP程序,一个是ServerSocket类,用于表示服务器端,一个是Socket类,用于表示客户端。

首先创建代表服务器端的ServerSocket对象,并等待客户端的连接,

然后创建代表客户端的Socket对象向服务器端发出连接请求,服务器端响应请求,客户端再次向服务器端发送确认信息,确认连接

public class TCPClient {//客户端
public static void main(String[] args) throws IOException {
Socket socket=new Socket("127.0.0.1",8888);//客户端对象 建连接
//Socket socket=new Socket(inet,8888);//(地址对象,端口号)
OutputStream out=socket.getOutputStream();//客户端的 输出流
//写数据
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
out.write(str.getBytes());
//明确数据源
FileInputStream fis=new FileInputStream("F:\\java\\1.jpg");
//BufferedInputStream bis = new BufferedInputStream(fis);
//写数据
int len=0;
byte[] bytes=new byte[1024];
while((len=fis.read(bytes))!=-1){ //bis.read(bytes)
out.write(bytes,0,len); //写入socket的 输出流
}
socket.shutdownOutput();//socket对象 的输出流 写入结束符
//接收服务器端的响应
InputStream in=socket.getInputStream();
byte[] bytes=new byte[1024];
int len=in.read(bytes);
System.out.println(new String(bytes,0,len));
socket.close();//释放内存
fis.close();//关闭数据源
}
}
int port=socket.getPort();//获取端口号
InetAddress inet=getLocalAddress();//获取本地地址对象
public class TCPServer {//服务器端
public static void main(String[] args) throws IOException {
ServerSocket server=new ServerSocket(8888);//服务端对象(端口号)
Socket socket=server.accept();//创建连接  获取客户端对象
InputStream in=socket.getInputStream();//客户端的 输入流  不关
//读出数据
byte[] bytes=new byte[1024];
int len=in.read(bytes);
System.out.println(new String(bytes,0,len));
//明确目的地
File file=new File("F:\\aaa");
if(!file.exists()){ file.mkdirs(); }//文件夹不存在  创建
String fn="oracle"+System.currentTimeMillis()//域名+毫秒值
+new Random().nextInt(999999)+".jpg";//+六位随机数+后缀
FileOutputStream fos=new FileOutputStream(file+File.separator+fn);
//BufferedOutputStream bos = new BufferedOutputStream(fos);
//开始复制
byte[] bytes=new byte[1024];
int len=0;
while((len=in.read(bytes))!=-1){
fos.write(bytes,0,len); //bos.write(bytes,0,len);
}
//发送给客户端响应
OutputStream out=socket.getOutputStream();//输出流
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
out.write(str.getBytes());//写数据
out.write("上传成功".getBytes());//写数据
server.close();//释放资源
fos.close(); //bos.close();
}
}
InetAddress address = socket.getInetAddress();//获取地址对象
String ip=address.getHostAddress(); //ip地址
String name=address.getHostName(); //主机名
System.out.println("来自IP:"+ip+"主机名:"+name);
多线程(多人同时访问服务器)public class Upload implementsRunnable{privateSocket socket;publicUpload(){}public Upload(Socket socket){this.socket=socket;}public voidrun() {
FileOutputStream fos=null;try{
InputStream in=socket.getInputStream();//客户端 输入流//明确目的地
File file=new File("F:\\aaa");if(!file.exists()){ file.mkdirs(); }//文件夹不存在 创建
String filename="oracle"+System.currentTimeMillis()//域名+毫秒值
+new Random().nextInt(999999)+".jpg";//+六位随机数+后缀
fos=new FileOutputStream(file+File.separator+filename);//开始复制
byte[] bytes=new byte[1024];int len=0;while((len=in.read(bytes))!=-1){fos.write(bytes,0,len);}//发送给客户端响应
OutputStream out=socket.getOutputStream();//输出流
out.write("上传成功".getBytes());//写数据
}catch(IOException ex){ex.printStackTrace();}finally{ if(fos!=null){fos.close();//点try...catch} }
}
}public class TCPThreadServer {//服务器端 多线程 (客户端不变)
public static void main(String[] args) throwsIOException {
ServerSocket server=new ServerSocket(8888);//服务端对象(端口号)
while(true){
Socket socket=server.accept();//创建连接 获取客户端对象
new Thread(new Upload(socket)).start();//创建并开启线程
}
}
}