A small client server problem

Alright I figured out my problem after I sent the int I had to flush my output stream buffere with flush().

One question is there a way to put code in some sort of code box here? I checked under help but didn’t find such a command.

I am just starting to learn client server programming in java. I sent some text between a client and server that was easy. I then tried to send some objects and other data and I couldn’t get it to work. If I make a loop for the client to send an int to the server. I have figured out through testing that it needs to try and send it at least 257 times before the server will ever recieve it anything below that and the server gets nothing. This makes absolutely no sense. My code shows my testing with the 257.

Client class
[source lang=“java”]
import java.net.;
import java.io.
;
import java.util.;
import javax.swing.
;

public class Client
{

//public Client(DefaultListModel model, ArrayList<String> summerTimes, ArrayList<String> timeOffSets,
				//int readType)
public static void main(String[] args)
{
	final int PORT = 6500;
	String server = "localhost";
	Socket socket = null;
	ObjectOutputStream oStream;
	ObjectInputStream iStream;
	int readType =1;

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

	try
	{
		oStream =  new ObjectOutputStream(socket.getOutputStream());
		iStream = new ObjectInputStream(socket.getInputStream());

		int x = 0;
		// get user input and transmit it to server
		while(true)
		{

			System.out.println("sending int");
			oStream.writeInt(1);
			x++;

			if (x == 257)
				break;
		}
	}
	catch (IOException e)
	{
		System.out.println(e);
	}

	try
	{
		socket.close();
	}
	catch (IOException e)
	{
		System.out.println(e);
	}
}

public void recieveInput()
{
	//model = iStream.readObject();
}

}
[/source]

Server Class
[source lang=“java”]
import java.net.;
import java.io.
;

public class Server
{
public static void main(String args[])
{

	final int PORT = 6500;
	ServerSocket server_socket;
	ObjectOutputStream oStream;
	ObjectInputStream iStream;
	int message = 0;

	System.out.println("Server Application");
	System.out.println("port = 6500 (default)");

	try
	{
		server_socket = new ServerSocket(PORT);
		System.out.println("Server waiting for client on port " +
				   server_socket.getLocalPort());

		// server infinite loop
		while(true)
		{
			Socket socket = server_socket.accept();
			System.out.println("New connection accepted " + socket.getInetAddress() + ":" +
								socket.getPort());
			oStream =  new ObjectOutputStream(socket.getOutputStream());
			iStream = new ObjectInputStream(socket.getInputStream());

			// print received data
			try
			{
				while(true)
				{
					message = iStream.readInt();
					if (message != 0)
						System.out.println("int recieved");

				}
			}
			catch (IOException e)
			{
				System.out.println(e);
			}

			// connection closed by client
			try
			{
				socket.close();
				System.out.println("Connection closed by client");
			}
			catch (IOException e)
			{
				System.out.println(e);
			}
		}
	}

	catch (IOException e)
	{
		System.out.println(e);
	}
}

}
[/source]

See the # icon in your toolbar? It will output matching tags that look like this (but with no spaces): [ code ][ /code ]

Actually it makes a lot of sense,. I suspect Nagle is getting you.

Try Socket.setTcpNoDelay(true);

And remember, the computer is (almost) always right… When you see somthing that makes no sense, it means one of your assumptions is wrong.