This program simply transmits the message entered at client to server.
TCP Server Program
tcpserver.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class tcpserver {
public static void main(String args[]) throws Exception
{
String message;
ServerSocket ssock = new ServerSocket(6789);
System.out.println("Server running...");
while(true)
{
Socket connsock = ssock.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(connsock.getInputStream()));
message=br.readLine();
System.out.println("Client sent : "+message); }
}
}
TCP Client Program
tcpclient.java
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;
public class tcpclient {
public static void main(String args[]) throws Exception
{
while(true){
String message;
BufferedReader inkbd = new BufferedReader(new InputStreamReader(System.in));
Socket csock = new Socket("127.0.0.1",6789);
DataOutputStream ou = new DataOutputStream(csock.getOutputStream());
System.out.println("Type message : ");
message = inkbd.readLine();
ou.writeBytes(message+"\n");
csock.close();
}
}
}