import java.io.*;
import java.net.*;
class SocketDemo_1
{
public static void main(String[] args) throws Exception
{
Socket s=new Socket("192.168.0.2",10004);
BufferedReader bufr=new BufferedReader(new FileReader("e:\\java123\\1.txt"));
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
String line=null;
while((line=bufr.readLine())!=null){
pw.println(line);
}
s.shutdownOutput();
BufferedReader bfr=new BufferedReader(new InputStreamReader(s.getInputStream()));
String str=bfr.readLine();
System.out.println(str);
bufr.close();
s.close();
}
}
import java.io.*;
import java.net.*;
class ServerDemo_1
{
public static void main(String[] args)throws Exception
{
ServerSocket ss=new ServerSocket(10004);
Socket s=ss.accept();
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip);
BufferedReader bufr=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out=new PrintWriter(new FileWriter("e:\\java123\\tcp\\Receive1.txt",true));
String line=null;
while((line=bufr.readLine())!=null){
out.println(line);
}
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println("上传成功");
out.close();
s.close();
ss.close();
}
}
|
|