/*
TCP通訊
[示例]:傳送文本文件 (客戶端)
*/
import java.net.*;
import java.io.*;
class Demo
{
public static void main(String[] args) throws Exception
{
new FileClient();
}
}
class FileClient //客戶端
{
FileClient() throws Exception
{
s.op("客戶端啟動(dòng)....");
client();
}
public void client()throws Exception
{
Socket sock = new Socket("192.168.1.3",10006);//指定服務(wù)器地址和接收端口
//將c盤一個(gè)文本文件發(fā)送到服務(wù)器端
BufferedReader bufr = new BufferedReader(new FileReader("c:\\abcd.java"));
//定義socket輸出流,將數(shù)據(jù)發(fā)給服務(wù)端
//BufferedWriter bufwOut=new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
//我們不用它了,用PrintWriter更方便,因?yàn)閜rintln方法自動(dòng)換行和刷新緩沖區(qū)9
PrintWriter priOut= new PrintWriter(sock.getOutputStream(),true);//將數(shù)據(jù)發(fā)送到socket輸出流
String fileLine = null;
while(true)
{
fileLine = bufr.readLine(); //讀文本文件
if(fileLine!=null)
{
priOut.println(fileLine); //將一行文本寫入socket輸出流
}
else
{
break;
}
}
//文件傳送完后,告訴服務(wù)端,"我發(fā)完了",也就是加一個(gè)結(jié)束標(biāo)記
//priOut.println("*#over886*#"); 這種方式不好,服務(wù)端怎么知道結(jié)束標(biāo)記是什么,不方便
sock.shutdownOutput(); //結(jié)束TCP套接字,之前寫入的數(shù)據(jù)都將被發(fā)送,并且后跟TCP連接終止標(biāo)記
BufferedReader bufrIn=new BufferedReader(new InputStreamReader(sock.getInputStream()));
String inStr = bufrIn.readLine(); //服務(wù)端此時(shí)應(yīng)該返回字符,比如"發(fā)送成功"
s.op(inStr); //顯示服務(wù)器返回的字符信息 "上傳成功."
bufr.close();
sock.close();
}
}
class s
{
public static void op(Object obj) //打印
{
System.out.println(obj);
}
}
TCP通訊
[示例]:傳送文本文件 (客戶端)
*/
import java.net.*;
import java.io.*;
class Demo
{
public static void main(String[] args) throws Exception
{
new FileClient();
}
}
class FileClient //客戶端
{
FileClient() throws Exception
{
s.op("客戶端啟動(dòng)....");
client();
}
public void client()throws Exception
{
Socket sock = new Socket("192.168.1.3",10006);//指定服務(wù)器地址和接收端口
//將c盤一個(gè)文本文件發(fā)送到服務(wù)器端
BufferedReader bufr = new BufferedReader(new FileReader("c:\\abcd.java"));
//定義socket輸出流,將數(shù)據(jù)發(fā)給服務(wù)端
//BufferedWriter bufwOut=new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
//我們不用它了,用PrintWriter更方便,因?yàn)閜rintln方法自動(dòng)換行和刷新緩沖區(qū)9
PrintWriter priOut= new PrintWriter(sock.getOutputStream(),true);//將數(shù)據(jù)發(fā)送到socket輸出流
String fileLine = null;
while(true)
{
fileLine = bufr.readLine(); //讀文本文件
if(fileLine!=null)
{
priOut.println(fileLine); //將一行文本寫入socket輸出流
}
else
{
break;
}
}
//文件傳送完后,告訴服務(wù)端,"我發(fā)完了",也就是加一個(gè)結(jié)束標(biāo)記
//priOut.println("*#over886*#"); 這種方式不好,服務(wù)端怎么知道結(jié)束標(biāo)記是什么,不方便
sock.shutdownOutput(); //結(jié)束TCP套接字,之前寫入的數(shù)據(jù)都將被發(fā)送,并且后跟TCP連接終止標(biāo)記
BufferedReader bufrIn=new BufferedReader(new InputStreamReader(sock.getInputStream()));
String inStr = bufrIn.readLine(); //服務(wù)端此時(shí)應(yīng)該返回字符,比如"發(fā)送成功"
s.op(inStr); //顯示服務(wù)器返回的字符信息 "上傳成功."
bufr.close();
sock.close();
}
}
class s
{
public static void op(Object obj) //打印
{
System.out.println(obj);
}
}
/*
這里我們沒有考慮客戶端的文件名,和客戶端判斷是否有重名文件,我們指定了文件名和路徑
[示例]:傳送文本文件 (服務(wù)端)
*/
import java.net.*;
import java.io.*;
class Demo
{
public static void main(String[] args) throws Exception
{
new FileServer();
}
}
class FileServer //服務(wù)端
{
FileServer() throws Exception
{
s.op("服務(wù)端啟動(dòng)......");
server();
}
public void server() throws Exception
{
ServerSocket serversock = new ServerSocket(10006);
Socket sock = serversock.accept();
String ip = sock.getInetAddress().getHostAddress();
s.op("來自客戶端IP "+ip+" 的文件");
BufferedReader bufrIn = new BufferedReader(new InputStreamReader(sock.getInputStream()));
PrintWriter priFileOut = new PrintWriter(new FileWriter("d:\\getFile.java"),true);
String inStr = null;
while(true)
{
inStr = bufrIn.readLine();
if(inStr!=null)
{
s.op(inStr); //將客戶端的文本數(shù)據(jù)打印到控制臺(tái)看看,對(duì)于大文件,本行代碼可注釋掉
priFileOut.println(inStr); //寫到文件中
}
else
{
break;
}
}
//文件保存完給客戶端一個(gè)返回信息
PrintWriter priOut = new PrintWriter(sock.getOutputStream(),true); //注意別丟了參數(shù)true
priOut.println("上傳成功.");//如果沒有true參數(shù),字符在緩沖區(qū)中不刷新的
sock.close();
priFileOut.close();
serversock.close();
}
}
class s
{
public static void op(Object obj) //打印
{
System.out.println(obj);
}
}
這里我們沒有考慮客戶端的文件名,和客戶端判斷是否有重名文件,我們指定了文件名和路徑
[示例]:傳送文本文件 (服務(wù)端)
*/
import java.net.*;
import java.io.*;
class Demo
{
public static void main(String[] args) throws Exception
{
new FileServer();
}
}
class FileServer //服務(wù)端
{
FileServer() throws Exception
{
s.op("服務(wù)端啟動(dòng)......");
server();
}
public void server() throws Exception
{
ServerSocket serversock = new ServerSocket(10006);
Socket sock = serversock.accept();
String ip = sock.getInetAddress().getHostAddress();
s.op("來自客戶端IP "+ip+" 的文件");
BufferedReader bufrIn = new BufferedReader(new InputStreamReader(sock.getInputStream()));
PrintWriter priFileOut = new PrintWriter(new FileWriter("d:\\getFile.java"),true);
String inStr = null;
while(true)
{
inStr = bufrIn.readLine();
if(inStr!=null)
{
s.op(inStr); //將客戶端的文本數(shù)據(jù)打印到控制臺(tái)看看,對(duì)于大文件,本行代碼可注釋掉
priFileOut.println(inStr); //寫到文件中
}
else
{
break;
}
}
//文件保存完給客戶端一個(gè)返回信息
PrintWriter priOut = new PrintWriter(sock.getOutputStream(),true); //注意別丟了參數(shù)true
priOut.println("上傳成功.");//如果沒有true參數(shù),字符在緩沖區(qū)中不刷新的
sock.close();
priFileOut.close();
serversock.close();
}
}
class s
{
public static void op(Object obj) //打印
{
System.out.println(obj);
}
}