could you post your code, if you don’t mind. i know about tcp/ip and
udp, but not really how to use them. a practical example could be
useful…
Well ok - It’s not exactly the code I actually use because for that I
would have to put a whole lot more classes here, so I stripped it down
to the essentials:
C++ Code first:
IPaddress m_serverAddress;
TCPsocket m_socket;
SDLNet_Init(); // somewhere to put …
//------------------------------------------------------------------------
bool connect() {
/*
Well - ok that three lines are to get the servers name and port which
-
in my case
come from an XML-config file. Just change your values for serverName
and serverPort
*/
CConfigManager *conf = CConfigManager::getInstance();
string serverName = conf->getString(“network”, “server”, “dns”);
short serverPort = (short)conf->getInt(“network”, “server”, “port”);
// This here gives you the IP-Adress of the server named in serverName
if(SDLNet_ResolveHost(&m_serverAddress, serverName.c_str(),
serverPort) == -1) {
cout << “Could not resolve host (” << serverName << “).” << endl <<
"SDL says: " << SDLNet_GetError() << endl;
return false;
}
// This opens the connection
if (!(m_socket = SDLNet_TCP_Open(&m_serverAddress))) {
cout << "Could not open connection to " << serverName << “:” <<
serverPort << “.” << endl << "SDL says: " << SDLNet_GetError() << endl;
return false;
}
cout << "Connected to " << serverName << “:” << serverPort << “.” <<
endl;
return true;
}
//
void disconnect() {
// Plain and simple - close your socket
SDLNet_TCP_Close(m_socket);
cout << “Disconnected.” << endl;
}
//
bool sendString(string &message) {
// Now let’s send a text over this connection
int send = SDLNet_TCP_Send(m_socket, message.c_str(),
message.length());
/*
My code looks a lot different here but this is the key function:
I’m not sure if length() exists (still looking for a good reference)
but it should be clear what I mean =)
*/
return (send<payloadlen);
}
I haven’t yet implemented a recieve Function because we haven’t yet
defined a protocol, but that should be straight forward reading from a
"stream".
Now to the Java-Server - that’s a bit easier
server.java:
import java.net.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Vector;
public class server implements Runnable {
public server(int _port) {
try {
m_serverSocket = new ServerSocket(_port); // Open a Socket
m_running = false;
}
catch (Exception e) {
System.out.println("Could not open serversocket for
port"+String.valueOf(_port)+".\nEvent says: "+e.getMessage());
}
}
public void run() {
try {
System.out.println("Waiting for connection ...");
// Nur eine Connection erlaubt
Socket clientConnection = m_serverSocket.accept(); // This blocks
the function (thread in this case) until someone connected
System.out.println(“Huston we’ve got contact …”);
// now we have to get a couple of streams because we want to read
from our connection
OutputStream snd_stream = clientConnection.getOutputStream();
InputStream rcv_stream = clientConnection.getInputStream();
m_running = true;
String message;
// some converting
BufferedReader rcv = new BufferedReader(new
InputStreamReader(rcv_stream));
System.out.println("Ready for action.");
// Main loop
while (m_running) {
message = rcv.readLine(); // now that's easy, read until \n
if (message.equals("quit"))
stop();
else
System.out.println(message);
}
System.out.println("Cleaning up...");
// close the connection
clientConnection.close();
m_serverSocket.close();
System.out.println("Bye");
}
catch (Exception e) {
System.out.println("Ooups ...\n"+e.getMessage());
}
}
// to stop our thread
public void stop() {
m_running = false;
}
private ServerSocket m_serverSocket;
private boolean m_running;
}
//
java_server_dummy.java:
public class java_server_dummy {
public static void main (String args[]) {
System.out.println("Here we go ...");
server s1 = new server(1234);
Thread s1Thread = new Thread(s1);
s1Thread.start();
}
}
What all that stuff does - well it’s simply a “client sends text to
server” app. There is no feedback from the server, but that should be
easy to implement.
Most of the C++ code is copied from the SDL_net docs and slightly
modified.
I hope that helps. Otherwise have a look at the SDL_net docs and/or
look up sockets on google.
BR Arne