Created
April 10, 2019 06:53
-
-
Save roshanadh/aac830109d0b7ab5260b19a84347bd89 to your computer and use it in GitHub Desktop.
Send messages to a server in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Client Program */ | |
import java.io.*; | |
import java.net.*; | |
public class TCPClient | |
{ | |
public static void main(String[] args) throws Exception{ | |
String clientData,processedData; | |
while(true) | |
{ | |
/* Creating Socket for the Client */ | |
Socket clientSocket=new Socket("127.0.0.1",2222); | |
/* Creating Output Stream for the Server */ | |
DataOutputStream outToServer=new DataOutputStream(clientSocket.getOutputStream()); | |
/* Creating Input Stream for the Client */ | |
BufferedReader inFromServer=new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); | |
System.out.println("Enter data: "); | |
/* Reading Message from the User */ | |
BufferedReader inFromUser=new BufferedReader(new InputStreamReader(System.in)); | |
clientData=inFromUser.readLine(); | |
/* Sending Message to the Server */ | |
outToServer.writeBytes(clientData+'\n'); | |
/* Reading Acknowledgement Message from the Server */ | |
processedData=inFromServer.readLine(); | |
System.out.println("From Server: "+processedData+"\n"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment