socket returning double input












0















I'm new to socket programming in Java and I made a simple client/server with sockets in Java where client need to input the options to access the function in server and I have this problem where the server send double input but I just need one.



Can anyone help me? thanks.



Server:



public static void main(String args) throws Exception {
try {
ServerSocket server = new ServerSocket(8888);
int counter = 0;
System.out.println("Server Started ....");
while (true) {
counter++;
Socket serverClient = server.accept();
// server accept the client connection request
System.out.println(" >> " + "Client No:" + counter + " started!");
DataInputStream inStream = new DataInputStream(serverClient.getInputStream());
DataOutputStream outStream = new DataOutputStream(serverClient.getOutputStream());
ServerClientThread sct = new ServerClientThread(serverClient, counter, inStream,
outStream);
// send the request to a separate thread
sct.start();
}
} catch (IOException e) {
System.out.println(e);
}
}

class ServerClientThread extends Thread {

final Socket serverClient;
final int clientNo;
final DataInputStream inStream;
final DataOutputStream outStream;


ServerClientThread(Socket inSocket, int counter, DataInputStream inStream,
DataOutputStream outStream) {
this.serverClient = inSocket;
this.clientNo = counter;
this.inStream = inStream;
this.outStream = outStream;
}


@Override
public void run() {
try {
String clientMessage = "", serverMessage = "";
while (!clientMessage.equals("bye")) {
outStream.writeUTF("1,2,3:");
clientMessage = inStream.readUTF();
switch (clientMessage) {
case "1":
outStream.writeUTF("masukkan number:");
outStream.flush();
serverMessage = inStream.readUTF();
outStream.writeUTF(serverMessage);
break;
default:
System.out.println("tidak ada pilihan!");
}
}
inStream.close();
outStream.close();
serverClient.close();
} catch (IOException ex) {
System.out.println(ex);
} finally {
System.out.println("Client -" + clientNo + " exit!! ");
}
}
}


Client:



public static void main(String args) throws Exception {
try {
Socket socket = new Socket("127.0.0.1", 8888);
DataInputStream inStream = new DataInputStream(socket.getInputStream());
DataOutputStream outStream = new DataOutputStream(socket.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String clientMessage = "", serverMessage = "";
while (!clientMessage.equals("bye")) {
System.out.println(inStream.readUTF());
String input = br.readLine();
outStream.writeUTF(input);
}
outStream.close();
outStream.close();
socket.close();
} catch (Exception e) {
System.out.println(e);
}
}


Error on Client Side:



 1,2,3:
1
masukkan number:
12
12
3
1,2,3:
1
1,2,3:
1
masukkan number:
12
1
13
1,2,3:
14
1,2,3:
15


Error on Server Side:



 Server Started ....
Client No:1 started!
tidak ada pilihan!
tidak ada pilihan!
tidak ada pilihan!
tidak ada pilihan!
tidak ada pilihan!
tidak ada pilihan!
java.net.SocketException: Connection reset
Client -1 exit!!









share|improve this question

























  • Please clarify which is "double input"?

    – xtratic
    Nov 20 '18 at 13:47











  • Use a debugger and follow what is happening with the server and client writing and reading.

    – xtratic
    Nov 20 '18 at 14:03











  • you can see in error client, Server should only send 1 input but it return 3 1,2,3: <<-- option 1<<--option select masukkan number: 12<client input 12<-server input 3<-here it return another value

    – user10109481
    Nov 20 '18 at 14:03













  • that 3 is client input again

    – xtratic
    Nov 20 '18 at 14:06











  • 1,2,3: (server message) 1 (client input and message) masukkan number: (server response) 12 (client input and message) 12 (server response) 3 (client input and message) 1,2,3: (server response)

    – xtratic
    Nov 20 '18 at 14:08
















0















I'm new to socket programming in Java and I made a simple client/server with sockets in Java where client need to input the options to access the function in server and I have this problem where the server send double input but I just need one.



Can anyone help me? thanks.



Server:



public static void main(String args) throws Exception {
try {
ServerSocket server = new ServerSocket(8888);
int counter = 0;
System.out.println("Server Started ....");
while (true) {
counter++;
Socket serverClient = server.accept();
// server accept the client connection request
System.out.println(" >> " + "Client No:" + counter + " started!");
DataInputStream inStream = new DataInputStream(serverClient.getInputStream());
DataOutputStream outStream = new DataOutputStream(serverClient.getOutputStream());
ServerClientThread sct = new ServerClientThread(serverClient, counter, inStream,
outStream);
// send the request to a separate thread
sct.start();
}
} catch (IOException e) {
System.out.println(e);
}
}

class ServerClientThread extends Thread {

final Socket serverClient;
final int clientNo;
final DataInputStream inStream;
final DataOutputStream outStream;


ServerClientThread(Socket inSocket, int counter, DataInputStream inStream,
DataOutputStream outStream) {
this.serverClient = inSocket;
this.clientNo = counter;
this.inStream = inStream;
this.outStream = outStream;
}


@Override
public void run() {
try {
String clientMessage = "", serverMessage = "";
while (!clientMessage.equals("bye")) {
outStream.writeUTF("1,2,3:");
clientMessage = inStream.readUTF();
switch (clientMessage) {
case "1":
outStream.writeUTF("masukkan number:");
outStream.flush();
serverMessage = inStream.readUTF();
outStream.writeUTF(serverMessage);
break;
default:
System.out.println("tidak ada pilihan!");
}
}
inStream.close();
outStream.close();
serverClient.close();
} catch (IOException ex) {
System.out.println(ex);
} finally {
System.out.println("Client -" + clientNo + " exit!! ");
}
}
}


Client:



public static void main(String args) throws Exception {
try {
Socket socket = new Socket("127.0.0.1", 8888);
DataInputStream inStream = new DataInputStream(socket.getInputStream());
DataOutputStream outStream = new DataOutputStream(socket.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String clientMessage = "", serverMessage = "";
while (!clientMessage.equals("bye")) {
System.out.println(inStream.readUTF());
String input = br.readLine();
outStream.writeUTF(input);
}
outStream.close();
outStream.close();
socket.close();
} catch (Exception e) {
System.out.println(e);
}
}


Error on Client Side:



 1,2,3:
1
masukkan number:
12
12
3
1,2,3:
1
1,2,3:
1
masukkan number:
12
1
13
1,2,3:
14
1,2,3:
15


Error on Server Side:



 Server Started ....
Client No:1 started!
tidak ada pilihan!
tidak ada pilihan!
tidak ada pilihan!
tidak ada pilihan!
tidak ada pilihan!
tidak ada pilihan!
java.net.SocketException: Connection reset
Client -1 exit!!









share|improve this question

























  • Please clarify which is "double input"?

    – xtratic
    Nov 20 '18 at 13:47











  • Use a debugger and follow what is happening with the server and client writing and reading.

    – xtratic
    Nov 20 '18 at 14:03











  • you can see in error client, Server should only send 1 input but it return 3 1,2,3: <<-- option 1<<--option select masukkan number: 12<client input 12<-server input 3<-here it return another value

    – user10109481
    Nov 20 '18 at 14:03













  • that 3 is client input again

    – xtratic
    Nov 20 '18 at 14:06











  • 1,2,3: (server message) 1 (client input and message) masukkan number: (server response) 12 (client input and message) 12 (server response) 3 (client input and message) 1,2,3: (server response)

    – xtratic
    Nov 20 '18 at 14:08














0












0








0








I'm new to socket programming in Java and I made a simple client/server with sockets in Java where client need to input the options to access the function in server and I have this problem where the server send double input but I just need one.



Can anyone help me? thanks.



Server:



public static void main(String args) throws Exception {
try {
ServerSocket server = new ServerSocket(8888);
int counter = 0;
System.out.println("Server Started ....");
while (true) {
counter++;
Socket serverClient = server.accept();
// server accept the client connection request
System.out.println(" >> " + "Client No:" + counter + " started!");
DataInputStream inStream = new DataInputStream(serverClient.getInputStream());
DataOutputStream outStream = new DataOutputStream(serverClient.getOutputStream());
ServerClientThread sct = new ServerClientThread(serverClient, counter, inStream,
outStream);
// send the request to a separate thread
sct.start();
}
} catch (IOException e) {
System.out.println(e);
}
}

class ServerClientThread extends Thread {

final Socket serverClient;
final int clientNo;
final DataInputStream inStream;
final DataOutputStream outStream;


ServerClientThread(Socket inSocket, int counter, DataInputStream inStream,
DataOutputStream outStream) {
this.serverClient = inSocket;
this.clientNo = counter;
this.inStream = inStream;
this.outStream = outStream;
}


@Override
public void run() {
try {
String clientMessage = "", serverMessage = "";
while (!clientMessage.equals("bye")) {
outStream.writeUTF("1,2,3:");
clientMessage = inStream.readUTF();
switch (clientMessage) {
case "1":
outStream.writeUTF("masukkan number:");
outStream.flush();
serverMessage = inStream.readUTF();
outStream.writeUTF(serverMessage);
break;
default:
System.out.println("tidak ada pilihan!");
}
}
inStream.close();
outStream.close();
serverClient.close();
} catch (IOException ex) {
System.out.println(ex);
} finally {
System.out.println("Client -" + clientNo + " exit!! ");
}
}
}


Client:



public static void main(String args) throws Exception {
try {
Socket socket = new Socket("127.0.0.1", 8888);
DataInputStream inStream = new DataInputStream(socket.getInputStream());
DataOutputStream outStream = new DataOutputStream(socket.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String clientMessage = "", serverMessage = "";
while (!clientMessage.equals("bye")) {
System.out.println(inStream.readUTF());
String input = br.readLine();
outStream.writeUTF(input);
}
outStream.close();
outStream.close();
socket.close();
} catch (Exception e) {
System.out.println(e);
}
}


Error on Client Side:



 1,2,3:
1
masukkan number:
12
12
3
1,2,3:
1
1,2,3:
1
masukkan number:
12
1
13
1,2,3:
14
1,2,3:
15


Error on Server Side:



 Server Started ....
Client No:1 started!
tidak ada pilihan!
tidak ada pilihan!
tidak ada pilihan!
tidak ada pilihan!
tidak ada pilihan!
tidak ada pilihan!
java.net.SocketException: Connection reset
Client -1 exit!!









share|improve this question
















I'm new to socket programming in Java and I made a simple client/server with sockets in Java where client need to input the options to access the function in server and I have this problem where the server send double input but I just need one.



Can anyone help me? thanks.



Server:



public static void main(String args) throws Exception {
try {
ServerSocket server = new ServerSocket(8888);
int counter = 0;
System.out.println("Server Started ....");
while (true) {
counter++;
Socket serverClient = server.accept();
// server accept the client connection request
System.out.println(" >> " + "Client No:" + counter + " started!");
DataInputStream inStream = new DataInputStream(serverClient.getInputStream());
DataOutputStream outStream = new DataOutputStream(serverClient.getOutputStream());
ServerClientThread sct = new ServerClientThread(serverClient, counter, inStream,
outStream);
// send the request to a separate thread
sct.start();
}
} catch (IOException e) {
System.out.println(e);
}
}

class ServerClientThread extends Thread {

final Socket serverClient;
final int clientNo;
final DataInputStream inStream;
final DataOutputStream outStream;


ServerClientThread(Socket inSocket, int counter, DataInputStream inStream,
DataOutputStream outStream) {
this.serverClient = inSocket;
this.clientNo = counter;
this.inStream = inStream;
this.outStream = outStream;
}


@Override
public void run() {
try {
String clientMessage = "", serverMessage = "";
while (!clientMessage.equals("bye")) {
outStream.writeUTF("1,2,3:");
clientMessage = inStream.readUTF();
switch (clientMessage) {
case "1":
outStream.writeUTF("masukkan number:");
outStream.flush();
serverMessage = inStream.readUTF();
outStream.writeUTF(serverMessage);
break;
default:
System.out.println("tidak ada pilihan!");
}
}
inStream.close();
outStream.close();
serverClient.close();
} catch (IOException ex) {
System.out.println(ex);
} finally {
System.out.println("Client -" + clientNo + " exit!! ");
}
}
}


Client:



public static void main(String args) throws Exception {
try {
Socket socket = new Socket("127.0.0.1", 8888);
DataInputStream inStream = new DataInputStream(socket.getInputStream());
DataOutputStream outStream = new DataOutputStream(socket.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String clientMessage = "", serverMessage = "";
while (!clientMessage.equals("bye")) {
System.out.println(inStream.readUTF());
String input = br.readLine();
outStream.writeUTF(input);
}
outStream.close();
outStream.close();
socket.close();
} catch (Exception e) {
System.out.println(e);
}
}


Error on Client Side:



 1,2,3:
1
masukkan number:
12
12
3
1,2,3:
1
1,2,3:
1
masukkan number:
12
1
13
1,2,3:
14
1,2,3:
15


Error on Server Side:



 Server Started ....
Client No:1 started!
tidak ada pilihan!
tidak ada pilihan!
tidak ada pilihan!
tidak ada pilihan!
tidak ada pilihan!
tidak ada pilihan!
java.net.SocketException: Connection reset
Client -1 exit!!






java sockets serversocket






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 13:36









xtratic

2,4691822




2,4691822










asked Nov 20 '18 at 12:43







user10109481




















  • Please clarify which is "double input"?

    – xtratic
    Nov 20 '18 at 13:47











  • Use a debugger and follow what is happening with the server and client writing and reading.

    – xtratic
    Nov 20 '18 at 14:03











  • you can see in error client, Server should only send 1 input but it return 3 1,2,3: <<-- option 1<<--option select masukkan number: 12<client input 12<-server input 3<-here it return another value

    – user10109481
    Nov 20 '18 at 14:03













  • that 3 is client input again

    – xtratic
    Nov 20 '18 at 14:06











  • 1,2,3: (server message) 1 (client input and message) masukkan number: (server response) 12 (client input and message) 12 (server response) 3 (client input and message) 1,2,3: (server response)

    – xtratic
    Nov 20 '18 at 14:08



















  • Please clarify which is "double input"?

    – xtratic
    Nov 20 '18 at 13:47











  • Use a debugger and follow what is happening with the server and client writing and reading.

    – xtratic
    Nov 20 '18 at 14:03











  • you can see in error client, Server should only send 1 input but it return 3 1,2,3: <<-- option 1<<--option select masukkan number: 12<client input 12<-server input 3<-here it return another value

    – user10109481
    Nov 20 '18 at 14:03













  • that 3 is client input again

    – xtratic
    Nov 20 '18 at 14:06











  • 1,2,3: (server message) 1 (client input and message) masukkan number: (server response) 12 (client input and message) 12 (server response) 3 (client input and message) 1,2,3: (server response)

    – xtratic
    Nov 20 '18 at 14:08

















Please clarify which is "double input"?

– xtratic
Nov 20 '18 at 13:47





Please clarify which is "double input"?

– xtratic
Nov 20 '18 at 13:47













Use a debugger and follow what is happening with the server and client writing and reading.

– xtratic
Nov 20 '18 at 14:03





Use a debugger and follow what is happening with the server and client writing and reading.

– xtratic
Nov 20 '18 at 14:03













you can see in error client, Server should only send 1 input but it return 3 1,2,3: <<-- option 1<<--option select masukkan number: 12<client input 12<-server input 3<-here it return another value

– user10109481
Nov 20 '18 at 14:03







you can see in error client, Server should only send 1 input but it return 3 1,2,3: <<-- option 1<<--option select masukkan number: 12<client input 12<-server input 3<-here it return another value

– user10109481
Nov 20 '18 at 14:03















that 3 is client input again

– xtratic
Nov 20 '18 at 14:06





that 3 is client input again

– xtratic
Nov 20 '18 at 14:06













1,2,3: (server message) 1 (client input and message) masukkan number: (server response) 12 (client input and message) 12 (server response) 3 (client input and message) 1,2,3: (server response)

– xtratic
Nov 20 '18 at 14:08





1,2,3: (server message) 1 (client input and message) masukkan number: (server response) 12 (client input and message) 12 (server response) 3 (client input and message) 1,2,3: (server response)

– xtratic
Nov 20 '18 at 14:08












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393251%2fsocket-returning-double-input%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown
























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393251%2fsocket-returning-double-input%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]