Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  A small client server problem  (Read 640 times)
0 Members and 1 Guest are viewing this topic.
Offline Grahf

Junior Newbie





« Posted 2006-02-14 18:29:09 »

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]
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #1 - Posted 2006-02-14 19:33:27 »

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

Java Game Console Project
Last Journal Entry: 12/17/04
Offline Jeff

JGO Coder




Got any cats?


« Reply #2 - Posted 2006-02-18 02:24:24 »

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.

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.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (15 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (33 views)
2014-08-16 06:45:27

Tekkerue (32 views)
2014-08-16 06:22:17

Tekkerue (20 views)
2014-08-16 06:20:21

Tekkerue (29 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (42 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!