i'm still trying to study on socket server and client programming. So i did this coding based on the tutorial i received. I managed to create thread for multi client interaction. However, i could not stop the loop in the in the client handler that keep displaying welcoming message that i made even after i made case for it.
How to stop the looping of welcoming message that has been made?
Server side
public class server {
public static void main(String[] args) throws IOException{
//server listen on port 4999
ServerSocket ss = new ServerSocket(4999);
//running loop to get client request
while(true){
Socket s = null;
try
{
//socket object receive incoming client requests
s = ss.accept();
System.out.println("New Client is connected :" + s);
//Obtaining input and out streams
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
System.out.println("Assigning new thread for this client");
//create new thread object
Thread t = new ClientHandler(s, dis, dos);
//Invoking start() method
t.start();
}
catch (Exception e){
s.close();
e.printStackTrace();
}
}
}
}
class ClientHandler extends Thread{
DateFormat fordate= new SimpleDateFormat("yyyy/MM/dd");
DateFormat fortime = new SimpleDateFormat("yyyy/MM/dd");
final DataInputStream dis;
final DataOutputStream dos;
final Socket s;
//Constructor
public ClientHandler(Socket s, DataInputStream dis,DataOutputStream dos){
this.s = s;
this.dis = dis;
this.dos = dos;
}
@Override
public void run() {
String received;
String toreturn;
while(true){
try{
//ask user his position
dos.writeUTF("WELCOME TO CREWCUTS SOCKET SERVER. \n" +
"Select either [Customer | BarberShop] \n" +
"Type Exit to terminate connection");
//get client's answer
received = dis.readUTF();
if(received.equals("Exit")){
System.out.println("Client " + this.s + "send exit");
System.out.println("Closing connection");
this.s.close();
System.out.println("Connection closed");
break;
}
//creating Date object
Date date = new Date();
//write on output stream based on the answer from client
switch (received){
case "Customer" :
toreturn = fordate.format(date);
dos.writeUTF(toreturn + "\n Welcome to Customer service of CREWCUTS Socket Server");
break;
case "BarberShop" :
toreturn = fordate.format(date);
dos.writeUTF(toreturn +"\n Welcome to BarberShop service of CREWCUTS Socket Server");
break;
default:
dos.writeUTF("Invalid input");
break;
}
}
catch (IOException e) {
e.printStackTrace();
}
}
try
{
//closing resource
this.dis.close();
this.dos.close();
}
catch(IOException e){
e.printStackTrace();
}
}
}
Client Side
public class client {
public static void main(String[] args) throws IOException{
try{
Scanner scn = new Scanner(System.in);
//establish connection to server port 4999 in localhost
Socket s = new Socket("localhost" ,4999);
//obtaining input and out streams
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
//loop for exchange of information between client and client handler
while(true)
{
System.out.println(dis.readUTF());
String tosend = scn.nextLine();
dos.writeUTF(tosend);
//if client send Exit, connection closed and break from loop
if(tosend.equals("Exit")){
System.out.println("Closing connection : " + s);
s.close();
System.out.println("Connection closed");
break;
}
//printing info as requested by client
String received = dis.readUTF();
System.out.println(received);
}
//closing resources
scn.close();
dis.close();
dos.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}
Just remove the welcome message out of loop both in client and server as below.
server.java
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.*;
import java.net.*;
public class server {
public static void main(String[] args) throws IOException{
//server listen on port 4999
ServerSocket ss = new ServerSocket(4999);
//running loop to get client request
while(true){
Socket s = null;
try
{
//socket object receive incoming client requests
s = ss.accept();
System.out.println("New Client is connected :" + s);
//Obtaining input and out streams
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
System.out.println("Assigning new thread for this client");
//create new thread object
Thread t = new ClientHandler(s, dis, dos);
//Invoking start() method
t.start();
}
catch (Exception e){
s.close();
e.printStackTrace();
}
}
}
}
class ClientHandler extends Thread{
DateFormat fordate= new SimpleDateFormat("yyyy/MM/dd");
DateFormat fortime = new SimpleDateFormat("yyyy/MM/dd");
final DataInputStream dis;
final DataOutputStream dos;
final Socket s;
//Constructor
public ClientHandler(Socket s, DataInputStream dis,DataOutputStream dos){
this.s = s;
this.dis = dis;
this.dos = dos;
}
@Override
public void run() {
String received;
String toreturn;
//ask user his position
try{
dos.writeUTF("WELCOME TO CREWCUTS SOCKET SERVER. \n" +
"Select either [Customer | BarberShop] \n" +
"Type Exit to terminate connection");
}
catch (IOException e) {
e.printStackTrace();
}
while(true){
try{
//get client's answer
received = dis.readUTF();
if(received.equals("Exit")){
System.out.println("Client " + this.s + "send exit");
System.out.println("Closing connection");
this.s.close();
System.out.println("Connection closed");
break;
}
//creating Date object
Date date = new Date();
//write on output stream based on the answer from client
switch (received){
case "Customer" :
toreturn = fordate.format(date);
dos.writeUTF(toreturn + "\n Welcome to Customer service of CREWCUTS Socket Server");
break;
case "BarberShop" :
toreturn = fordate.format(date);
dos.writeUTF(toreturn +"\n Welcome to BarberShop service of CREWCUTS Socket Server");
break;
default:
dos.writeUTF("Invalid input");
break;
}
}
catch (IOException e) {
e.printStackTrace();
}
}
try
{
//closing resource
this.dis.close();
this.dos.close();
}
catch(IOException e){
e.printStackTrace();
}
}
}
client.java
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.*;
import java.util.Scanner;
import java.net.*;
public class client {
public static void main(String[] args) throws IOException{
try{
Scanner scn = new Scanner(System.in);
//establish connection to server port 4999 in localhost
Socket s = new Socket("localhost" ,4999);
//obtaining input and out streams
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
try{
System.out.println(dis.readUTF());
}
catch (Exception e){
e.printStackTrace();
}
//loop for exchange of information between client and client handler
while(true)
{
String tosend = scn.nextLine();
dos.writeUTF(tosend);
//if client send Exit, connection closed and break from loop
if(tosend.equals("Exit")){
System.out.println("Closing connection : " + s);
s.close();
System.out.println("Connection closed");
break;
}
//printing info as requested by client
String received = dis.readUTF();
System.out.println(received);
}
//closing resources
scn.close();
dis.close();
dos.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}