Click here to Skip to main content
15,885,366 members
Home / Discussions / Java
   

Java

 
QuestionJava Gui Project Pin
Member 140826818-Dec-18 22:26
Member 140826818-Dec-18 22:26 
AnswerRe: Java Gui Project Pin
Peter_in_27808-Dec-18 23:09
professionalPeter_in_27808-Dec-18 23:09 
AnswerRe: Java Gui Project Pin
Richard MacCutchan9-Dec-18 2:00
mveRichard MacCutchan9-Dec-18 2:00 
QuestionDatabase Connection from Java Pin
prithaa8-Dec-18 6:07
prithaa8-Dec-18 6:07 
GeneralRe: Database Connection from Java Pin
Richard MacCutchan8-Dec-18 6:54
mveRichard MacCutchan8-Dec-18 6:54 
Questionhelp turning code into gui Pin
Member 140790227-Dec-18 11:55
Member 140790227-Dec-18 11:55 
AnswerRe: help turning code into gui Pin
Richard MacCutchan7-Dec-18 22:21
mveRichard MacCutchan7-Dec-18 22:21 
QuestionObjectInputStream block on server side Pin
Valentinor4-Dec-18 4:42
Valentinor4-Dec-18 4:42 
Hi,

I'll start with the code for both server and client, it is just a simple example:

Server
Java
serverSocket = new ServerSocket(4554);
serverRunning = true;
while (serverRunning) {
    socket = serverSocket.accept();
    ObjectOutputStream dataOut = new ObjectOutputStream(socket.getOutputStream());
    ObjectInputStream dataIn = new ObjectInputStream(socket.getInputStream());
    System.out.println("Done 1");

    String string = dataIn.readUTF();
    System.out.println("Done 2");

    int number = dataIn.readInt();
    System.out.println("Done 3");

    dataOut.writeUTF("String from server");
    System.out.println("Done 4");

    dataOut.writeInt(1);
    System.out.println("Done 5");

    System.out.println(string + " | " + number);
}

Client
Java
Socket socket = new Socket("localhost", 4554);
ObjectOutputStream dataOut = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream dataIn = new ObjectInputStream(socket.getInputStream());
System.out.println("Done 1");

dataOut.writeUTF("String from client");
System.out.println("Done 2");

dataOut.writeInt(2);
System.out.println("Done 3");

String string = dataIn.readUTF();
System.out.println("Done 4");

int number = dataIn.readInt();
System.out.println("Done 5");

socket.close();
System.out.println(string + " | " + number);

According to java Object Serialization, you need to create the ObjectOutputStream before ObjectInputStream in both Server and Client side, which I did, but the Client will run until it gets to the point of having to wait for data from the server (print the message "Done 3" then wait), but on the Server side, the code only runs until it gets to the point of reading the first thing from the Client (print "Done 1" then wait).
What else I need to do, beside creating the ObjectOutputStream before ObjectInputStream?

modified 4-Dec-18 10:49am.

Questionresearch methodology Pin
Member 140745404-Dec-18 1:45
Member 140745404-Dec-18 1:45 
AnswerRe: research methodology Pin
Richard MacCutchan4-Dec-18 2:40
mveRichard MacCutchan4-Dec-18 2:40 
Questionjava Pin
GiteHrudaya3-Dec-18 18:38
GiteHrudaya3-Dec-18 18:38 
QuestionRe: java Pin
Richard MacCutchan3-Dec-18 22:01
mveRichard MacCutchan3-Dec-18 22:01 
QuestionWrite a Test to check if some config records exist in database Pin
maryam.saboor30-Nov-18 21:41
professionalmaryam.saboor30-Nov-18 21:41 
AnswerRe: Write a Test to check if some config records exist in database Pin
Richard MacCutchan30-Nov-18 22:23
mveRichard MacCutchan30-Nov-18 22:23 
GeneralRe: Write a Test to check if some config records exist in database Pin
maryam.saboor30-Nov-18 23:14
professionalmaryam.saboor30-Nov-18 23:14 
GeneralRe: Write a Test to check if some config records exist in database Pin
Richard MacCutchan30-Nov-18 23:19
mveRichard MacCutchan30-Nov-18 23:19 
GeneralRe: Write a Test to check if some config records exist in database Pin
maryam.saboor30-Nov-18 23:49
professionalmaryam.saboor30-Nov-18 23:49 
QuestionFind and replace sTring in Docx and Doc file using Java code Pin
Member 1406885427-Nov-18 1:26
Member 1406885427-Nov-18 1:26 
SuggestionRe: Find and replace sTring in Docx and Doc file using Java code Pin
Richard MacCutchan27-Nov-18 1:38
mveRichard MacCutchan27-Nov-18 1:38 
Questionjava Pin
mohit gite14-Nov-18 23:16
mohit gite14-Nov-18 23:16 
AnswerRe: java Pin
Richard MacCutchan15-Nov-18 0:29
mveRichard MacCutchan15-Nov-18 0:29 
GeneralRe: java Pin
jschell21-Nov-18 7:41
jschell21-Nov-18 7:41 
GeneralRe: java Pin
Richard MacCutchan21-Nov-18 22:06
mveRichard MacCutchan21-Nov-18 22:06 
GeneralRe: java Pin
jschell15-Dec-18 6:05
jschell15-Dec-18 6:05 
GeneralRe: java Pin
mohit gite21-Nov-18 23:54
mohit gite21-Nov-18 23:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.