Transfer text using Socket, tcp client - Java Network

Java examples for Network:TCP

Description

Transfer text using Socket, tcp client

Demo Code

import java.io.*;
import java.net.*;

class TCPClient {
     public static void main(String args[]) throws IOException {
          String sentence;/*w  w  w .  j a va 2  s.c o  m*/
          String modifiedSentence;
          BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
          try {
               Socket cs = new Socket("localhost",1234); // This can throw exceptions
               DataOutputStream outToServer = new DataOutputStream(cs.getOutputStream());
               BufferedReader inFromServer = new BufferedReader(new InputStreamReader(cs.getInputStream()));
               sentence = inFromUser.readLine();
               outToServer.writeBytes(sentence + '\n');
               modifiedSentence = inFromServer.readLine();
               System.out.println("Output from Server : " + modifiedSentence);
               cs.close();
          } catch (ConnectException e) {
               System.err.println("Exception:" + "\n" + e.getMessage());
          }
     }
}

Related Tutorials