您的位置:首页 > 理论基础 > 计算机网络

Tcp Socket 应用DEMO

2011-06-19 15:46 169 查看
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class TcpClient {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
if(args.length<3){
System.out.println("Usage: ServerIP ServerPort FilePath");
System.out.println("请输入服务器IP地址,端口号,本地文件的绝对路径!");
return;
}

Socket s;
try {
s = new Socket(args[0],Integer.parseInt(args[1]));
InputStream ips=s.getInputStream();
OutputStream ops=s.getOutputStream();
ResultShow(ips,ops,args[2]);

} catch (Exception e) {
e.printStackTrace();
}

}

private static void ResultShow(InputStream ips,OutputStream ops,String sourceFilePath){

try{
BufferedReader brNet=new BufferedReader(new InputStreamReader(ips));
String strWord=null;
while(true){
strWord=brNet.readLine();
if(strWord.equalsIgnoreCase("quite")){
System.out.println("上传成功");
ops.close();
break;
}else if(strWord.equalsIgnoreCase("start")){
FileOutput(ops,sourceFilePath);
System.out.println("传送字节到服务器");
}
System.out.println(strWord);

}
brNet.close();
}catch(Exception e){
e.printStackTrace();
}
}

private static void FileOutput(OutputStream ops,String sourceFilePath){
try {

FileInputStream fin=new FileInputStream(sourceFilePath);

int b=0;
byte[] buffer=new byte[1024];
//读取文件流
while(true){
//写入文件流
b=fin.read(buffer);
if(b!=-1) {
ops.write(buffer,0, b);
}else{
ops.flush();
return;
}
}

} catch (Exception e) {
e.printStackTrace();
}
}
}

------------------------------------------------------------------------------------------------------------------------------

import java.io.File;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpService {

/**
* @param args
*/
//在运行服务器端程序时需要设置【端口号】,【文件保存路径】,【文件扩展名】
//默认设置为Port:【8001】 SaveFilePath:【D://SaveUpFile】 FileExtension:【tmp】;
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
System.out.println("Usage: Port SaveFilePath FileExtension");
System.out.println("DefaultInfo Port:【8001】 SaveFilePath:【D://SaveUpFile】 FileExtension:【tmp】");

System.out.println("默认端口号:【8001】文件保存路径:【D://SaveUpFile】文件扩展名:【tmp】");
System.out.println("等待客户端发送文件。。。");
ServerSocket server;
if(args.length>2){
server=new ServerSocket(Integer.parseInt(args[0]));
}else{
server=new ServerSocket(8001);
}

boolean isRuning=true;
String filePath;
String ext;
if(args.length>2){
filePath=args[1];
ext="."+args[2];
}else{
filePath="D://SaveUpFile";
ext=".tmp";
}
CreateDirecorty(filePath);
while(isRuning){
//等待客户端发送请求,与客户端建立专线连接
Socket clientSocket=server.accept();
new Thread(new TcpFileHandle(clientSocket,filePath,ext)).start();
}
server.close();

}catch(Exception e){
e.printStackTrace();
}
}

public static void CreateDirecorty(String path) {
File targetMkDir=new File(path);
if(!targetMkDir.exists()){
try {
targetMkDir.mkdirs();
} catch (Exception e) {
e.printStackTrace();
}
}
}

}

-----------------------------------------------------------------------------------------------------------------------------------------------

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.sql.Timestamp;
import java.util.UUID;

public class TcpFileHandle implements Runnable{
private Socket clientSocket=null;
private String filePath;
private String ext;
public TcpFileHandle(Socket s,String filePath,String ext){
this.clientSocket=s;
this.filePath=filePath+"//";
this.ext=ext;
}

public void run() {
try{
//获取输入流
InputStream ips=clientSocket.getInputStream();
//获取输出流
OutputStream ops=clientSocket.getOutputStream();
//打印
PrintWriter pw=new PrintWriter(ops,true);

File f=new File(filePath+UUID.randomUUID()+ext);
if(!f.exists())
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream fs=new FileOutputStream(f);

byte[] buf=new byte[1024];
int len=0;

int count=1;
pw.println("start");
while(true){
len= ips.read(buf);
if(len!=-1){
pw.println("服务器正在进行第["+count+++"]次处理!");
fs.write(buf, 0, len);

if(len<1024){
pw.println("quite");
fs.flush();
fs.close();
ips.close();
//ops.close();
pw.close();
clientSocket.close();
return;
}
}
}
}catch(Exception e){
e.printStackTrace();
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: