learning TCP communication of socket
TCP
1.
2.
the code is as follows
package com.fuge.TCP.demo2;
import java.io.*;
import java.net.Socket;
public class UploadClient {
public static void main(String[] args) throws IOException {
//1. tcpsocket
Socket socket = new Socket("127.0.0.1", 6060);
//2.
BufferedReader bufr = new BufferedReader(new FileReader("D://test.txt"));
//3. TCP
//
PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
String line;
// TCP
while ((line = bufr.readLine()) != null) {
out.write(line);
/**
*
*/
out.flush();
}
//
socket.shutdownOutput();
//
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String returnMsg = in.readLine();
System.out.println(returnMsg);
//
bufr.close();
socket.close();
}
}
package com.fuge.TCP.demo2;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class UploadServer {
public static void main(String[] args) throws IOException {
// 1. TCP
ServerSocket ss = new ServerSocket(6060);
// socket
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostAddress() + "............connection");
//
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
// D
BufferedWriter bw = new BufferedWriter(new FileWriter("D://new.txt"));
String line;
while ((line = in.readLine()) != null) {
bw.write(line);
/**
*
* debug line
*/
bw.newLine();
bw.flush();
}
//
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
out.write("");
out.flush(); //
//
bw.close();
s.close();
ss.close();
}
}