TCP Socket one-to-one communication example
TCP Socket one-to-one communication example
talkserver.java
talkclient.java
talkserver.java
import java.io. *; import java.net.*; import java.applet.Applet; public class talkserver { public static void main(String args[]) { try { ServerSocket server = null; try { server = new ServerSocket(4700); }catch(Exception e) { System.out.println("can not listen to:" + e); } Socket socket = null; try { socket = server.accept(); }catch(Exception e) { System.out.println("Error:" + e); } String line; BufferedReader is = new BufferedReader(new InputStreamReader( socket.getInputStream())); PrintWriter os = new PrintWriter(socket.getOutputStream()); BufferedReader sin = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Client:" + is.readLine()); line = sin.readLine(); while (!line.equals("bye")) { os.println(line); os.flush(); System.out.println("Server:" + line); System.out.println("Client:" + is.readLine()); line = sin.readLine(); } is.close(); os.close(); socket.close(); server.close(); }catch(Exception e) { System.out.println("Error" + e); } } }
talkclient.java
import java.io. *; import java.net.*; public class talkclient { public static void main(String args[]) { try { Socket socket = new Socket("127.0.0.1",4700); BufferedReader sin = new BufferedReader(new InputStreamReader(System.in)); PrintWriter os = new PrintWriter(socket.getOutputStream()); BufferedReader is = new BufferedReader(new InputStreamReader( socket.getInputStream())); String readline; readline = sin.readLine(); while (!readline.equals("bye")) { os.println(readline); os.flush(); System.out.println("Client:" + readline); System.out.println("Server:" + is.readLine()); readline = sin.readLine(); } os.close(); is.close(); socket.close(); }catch(Exception e) { System.out.println("Error" + e); } } }
F:\java\socket>javac talkserver.java F:\java\socket>java talkserver Client: hello how are you Server: How are you? Client: What are you doing? watching a movie, how about you? Server: Watching a movie, how about you? Client: I listen to music, so boring, I also want to watch movies, do you have any movies you recommend? There is a new movie recently, which has just been released and has a good reputation. I recommend you to watch it. Server: There is a new movie recently, which has just been released and has a good reputation. I recommend you to watch it. Client: What movie? "The Great Cause of Founding an Army", do you want to see it? Server: "The Founding of an Army", do you want to watch it? Client: Oh, I've heard it. There's a hot show in this movie theater. I just have time this weekend, so I'll go check it out. How about you? I'm free this weekend too, let's watch it together. Server: I'm free this weekend, let's watch it together. Client: Wow, see you at the weekend. See Weekend Server: see you this weekend Client:null bye F:\java\socket>
F:\java\socket>javac talkclient.java F:\java\socket>java talkclient Hello Client: hello Server: How are you? What are you doing? Client: What are you doing? Server: Watching a movie, how about you? I listen to music, it's so boring, I want to watch movies too, do you have any movies you recommend? Client: I listen to music, so boring, I also want to watch movies, do you have any movies you recommend? Server: There is a new movie recently, which has just been released and has a good reputation. I recommend you to watch it. what movie? Client: What movie? Server: "The Founding of an Army", do you want to watch it? Oh, I heard that there is a hot show in this movie theater. I just have time this weekend, so I will go and see it. How about you? Client: Oh, I've heard it. There's a hot show in this movie theater. I just have time this weekend, so I'll go check it out. How about you? Server: I'm free this weekend, let's watch it together. Wow, see you on the weekend. Client: Wow, see you at the weekend. Server: see you this weekend bye F:\java\socket>