Synchronize with sockets

G

Greyham

I'm having a problem with sending data from a Java server to a java
client. The server writes data (just 50 incrementing ints) to a data
output stream and the client reads it and prints the result. The
problem I think is that the reader is faster than the writer so when I
print the output, I get something like: 0 0 0 0 0 0 0 1 0 0 0 2 0 0 0
3 0 0 0 4 0 0 0 5 0 0 .... etc until 0 0 24 or something when (in my
case) 100 ints have been outputted to the screen. Is there any way to
synchronize the two programs (other than getting the client to read
off every 4th number)? Here's the code for the server and client so
you can try it I guess:

import java.net.*;
import java.io.*;

public class tcpServerData {

public static void main(String args[]) {

int port;
ServerSocket server_socket;
BufferedReader input;
boolean keeprunning = true;
String lineToBeSent;
DataOutputStream output;

// initializing data we want to send to client
int[] data=new int[50];
for(int i=0;i<data.length;i++){
data=i;
}

try {
port = Integer.parseInt(args[0]);//port is set to argument from
command line
}
catch (Exception e) {
System.out.println("port = 1500 (default)");
port = 1500;
}
try {
server_socket = new ServerSocket(port);
System.out.println("Server waiting for client on port " +
server_socket.getLocalPort());
while(keeprunning) {
Socket socket = server_socket.accept();
System.out.println("New connection accepted " +
socket.getInetAddress() +
":" + socket.getPort());
System.out.println("Type 'q' to quit");
try {
input = new BufferedReader(new
InputStreamReader(System.in));//converts imput stream
(bytes)>chars|bufferedreader
output = new DataOutputStream(socket.getOutputStream()/*,true*/);//returns
output stream for writing bytes to "socket"

// write data to the output stream
for(int i=0;i<data.length;i++) {
output.writeInt(data);
}//for
}
catch (IOException e) {
System.out.println(e);
}
// connection closed by client
try {
socket.close();
System.out.println("Connection closed");
}
catch (IOException e) {
System.out.println(e);
}
keeprunning = false;
}
}
catch (IOException e) {
System.out.println(e);
}
}
}
--------------------Here's the client code------------------
import java.net.*;
import java.io.*;

public class tcpClientData {

public static void main(String[] args) {
// declaring variables
int port = 1500;
String server = "localhost";
Socket socket = null;
String lineToBeSent;
BufferedInputStream input;
PrintWriter output;
int ERROR = 1;
int[] numbers = new int[100];
int i=0;
for(i=0;i<numbers.length;i++) {
numbers=999;
}

// read arguments to set host and port
if(args.length == 2) {
server = args[0];
try {
port = Integer.parseInt(args[1]);
}
catch (Exception e) {
System.out.println("server port = 1500 (default)");
port = 1500;
}
}

// connect to server
try {
socket = new Socket(server, port);
System.out.println("Connected with server " +
socket.getInetAddress() +
":" + socket.getPort());
}
catch (UnknownHostException e) {
System.out.println(e);
System.exit(ERROR);
}
catch (IOException e) {
System.out.println(e);
System.exit(ERROR);
}

// print received data
try {
//bytes>chars|bufferedreader
input = new BufferedInputStream(socket.getInputStream());
i=0;
while(i<numbers.length) {
numbers=input.read();
i++;
}//while
for(i=0;i<numbers.length;i++){
System.out.println(numbers);
}//for
}//try
catch (IOException e) {
System.out.println(e);
}
//closing connection
try {
socket.close();
}
catch (IOException e) {
System.out.println(e);
}
}
}

Sorry it's all quite a mess and thanks for looking.
 
L

Lee Fesperman

Greyham said:
I'm having a problem with sending data from a Java server to a java
client. The server writes data (just 50 incrementing ints) to a data
output stream and the client reads it and prints the result. The
problem I think is that the reader is faster than the writer so when I
print the output, I get something like: 0 0 0 0 0 0 0 1 0 0 0 2 0 0 0
3 0 0 0 4 0 0 0 5 0 0 .... etc until 0 0 24 or something when (in my
case) 100 ints have been outputted to the screen. Is there any way to
synchronize the two programs (other than getting the client to read
off every 4th number)? Here's the code for the server and client so
you can try it I guess:


public class tcpServerData {

...
output = new DataOutputStream(socket.getOutputStream()/*,true*/);//returns
output stream for writing bytes to "socket"

// write data to the output stream
for(int i=0;i<data.length;i++) {
output.writeInt(data);
}//for
...
--------------------Here's the client code------------------

...
input = new BufferedInputStream(socket.getInputStream());
i=0;
while(i<numbers.length) {
numbers=input.read();
i++;
...


Try using DataInputStream for the client ... since you're using DataOutputStream in the
server.
 
S

Steve Horsley

Greyham said:
I'm having a problem with sending data from a Java server to a java
client. The server writes data (just 50 incrementing ints) to a data
output stream and the client reads it and prints the result. The
problem I think is that the reader is faster than the writer so when I
print the output, I get something like: 0 0 0 0 0 0 0 1 0 0 0 2 0 0 0
3 0 0 0 4 0 0 0 5 0 0 .... etc until 0 0 24 or something when (in my
case) 100 ints have been outputted to the screen. Is there any way to
synchronize the two programs (other than getting the client to read
off every 4th number)? Here's the code for the server and client so
you can try it I guess:
<code snipped>

Since you are using DataOutputStream.writeInt() to send the values,
(which are 4-byte values by the way), why not use
DataInputStream.readInt() to read them back?

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top