[java and c++] server client port listening and reacting

Started by
4 comments, last by hplus0603 11 years, 11 months ago
I've always had troubles with network programming, so I'll try and keep my goal short and sweet.
The Client (Java Applet)
will send byte arrays to the server, the first byte saying the information that is sent, the following bytes that information.
E.G. send a byte 1 asking "can move into area?" followed by the area coordinate.
Then waits for a reply from the server before taking action
E.G receives a byte 0 meaning "no its a wall"

the format I imagine something like:

onKeyPress(key)
{
if (!moving)
{
SocketManager.sendQuery(1, 25, 46);
Byte[] query;
double Start = System.getNanoseconds();
while ((query = SocketManager.queryReply()) == null)
{
if (System.getNanoseconds() >= Start + Timeout)
{
Error("Connection Timed Out");
return;
}
}
if (query[0] == 1)
{
Move(25, 46);
}
}
}


The Server (C++ server)
simply takes the queries sent by the client and answers them, occasionally checking for connection time outs and letting clients know if another player is near by (so they can draw it on screen and interact with it)

does anyone have any good sources / tutorials for this?
I've found a few which are poorly commented and simply say "this bunch of lines does this" when I would like to know what the functions and arguments do and what are the options etc.

Any and all help appreciated,
thanks in advanced,
Bombshell
Advertisement
The actual documentation for the java nio classes is probably what you want.
Here's a starting point:
http://java.sun.com/j2se/1.4/nio/index.html
enum Bool { True, False, FileNotFound };
thanks I've started making progress in socket listening and sending.
My main concern now is, is there anything I need to keep in mind when networking between languages?(C++ and Java) I assumed they both read and send packets a similar way, so I assume I can get by via filling a an array of bytes / chars with the received data and interpreting it byte by byte as normal.
is there anything I need to keep in mind when networking between languages?(C++ and Java)[/quote]

The three things I can think of:
- Java serialization is not available in C++.
- The byte order (endian-ness) or representation used to store native types (like integer, float, etc) may be different, so raw memory copying won't work.
- When you don't use the same code to implement a protocol on a server and a client, they are much more likely to get out of sync and cause protocol errors.
enum Bool { True, False, FileNotFound };
1. is it possible to just send byte arrays via java, or does it serialize any structure or set of structures it sends?
2. looking at it, java uses UTF-8 for its byte, so if I can use UTF-8 in C++ little if any conversion would be needed. alternatively I could reinterpret the UTF-8 sent by java but, using UTF-8 standard in C++ would no doubt be faster and save some trouble,
3. does this mean I need to use a cross language wrapper? or just make sure the protocol's are set up similarly?
I aim (as pointed out in original post) to simply query the server, wait for server response and continue accordingly,
no game time is needed so in what sense would I need synchronising? (you probably mean something else, I'd like to stress how little knowledge I have in network programming XD)
1. is it possible to just send byte arrays via java, or does it serialize any structure or set of structures it sends?
2. looking at it, java uses UTF-8 for its byte, so if I can use UTF-8 in C++ little if any conversion would be needed. alternatively I could reinterpret the UTF-8 sent by java but, using UTF-8 standard in C++ would no doubt be faster and save some trouble,
3. does this mean I need to use a cross language wrapper? or just make sure the protocol's are set up similarly?
[/quote]

1. Yes, you can send byte arrays. If all you do is use sockets, you're dealing with bytes. The Java serialization and remoting support lives in different libraries.
2. Java uses UTF-8 when serializing strings to bytes. This means that, if you want to send a string, and receive it on the other end, you will need to decode it as UTF-8. Or just keep it in the UTF-8 format -- that's a fine format for string data in C++.
3. It means you have to define the protocol so that it describes *exactly the same* bytes on the wire in both languages. This is certainly possible, it's done every day, and it's why protocols are usually "better" than APIs. However, it is a bit of work.

enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement