Socket Programming(Help me!!!)

Hi,

I am working on my battleship program. However i am required to use socket programming to implement the messaging between the player and server.

The problem is, i have never done socket programming. How do i start?

Anyone care to help?

Last edited on
http://lacewing-project.org/
I'd start with this simple little library. I can provide examples if the documentation isn't self explanitory.

As a bare minimum, you'd need this code:
1
2
3
4
5
6
7
8
9
10
11
12
//Server
#include "Lacewing.h"

int main()
{
    Lacewing::EventPump EventPump;
    Lacewing::RelayServer Server (EventPump); //RelayServer has a protocol already for you
    Server.Host(6121);
    EventPump.StartEventLoop();
    //Done! Server will host as long as the program runs
    //For special server-side behavior you need to register handlers as in the docs
}

Edit: Here is the client bare minimum:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Client
#include "Lacewing.h"

int main()
{
    Lacewing::EventPump EventPump;
    Lacewing::RelayClient Self (EventPump); //RelayClient to connect to RelayServer
    Self.Connect("127.server.ip.0", 6121);
    //you need to register handlers to further initiate the connection
    //(setting name, joining channels, etc.)
    do
    {
        EventPump.Tick(); //rarely returns an error, see documentation
        //Game Code
    }while(/*game loop condition*/);
}


This is the simplest library I have found...
Last edited on
Thanks LB. However, i need to use the actual communicating codes for my assignment.

Anyway I managed to setup the server and client socket. Now i need to move on to the communicating part. what are the codes for server and client to communicate?
What do you mean "codes"? If you're using Lacewing::Server and Lacewing:Client, or just Lacewing::Socket, then you have to decide on packet codes yourself.

Unless by "codes" you mean "code"? There's a big difference.
In that case, the site has documentation that explains everything in depth...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
int main(int argc, char** argv) {


    //creating server
    struct sockaddr_un serverAddress;
    int serverSocket, clientSocket;

    serverSocket = socket(AF_LOCAL, SOCK_STREAM, 0);

    serverAddress.sun_family = AF_LOCAL;

    strcpy(serverAddress.sun_path, "GameServer");

    unlink("GameServer");

    bind(serverSocket, (struct sockaddr *) &serverAddress, SUN_LEN(&serverAddress));

    listen(serverSocket, 5);

    //client socket
    clientSocket = socket(PF_LOCAL, SOCK_STREAM, 0);

    connect(clientSocket, (struct sockaddr *) &serverAddress, SUN_LEN(&serverAddress));

    for(; ;)
    {
        clientSocket = accept(serverSocket, (sockaddr *) NULL, NULL);
    
        Computer c; 
        c.arrangeShips();
        c.saveInFile();

        //code for communication between server and client.

        shutdown(clientSocket, 2);
        close(clientSocket);   
    }
    return 0;
}


Here's the code i have tried to came up with. Am I on the right track?
What library are you using?
I using

1
2
3
#include <sys/ type.h>
#include <sys/ socket.h>
#include <sys/ un.h> 


My program requires the client player to enter a target square (eg. A3) in the server's grid which is to be shot at. if a ship occupies the square, then it takes a hit, the server opponent will reply whether or not the shot hit one of its ships and type of warship will be revealed using the symbol code.

The desired output should be like this,

Client's console
Enter a square on the battle grid to bomb:
A3

Server's console
Client bombed on square - A3
A miss!

Client's console
A miss!
Pls enter another square to bomb or press V to view your battle grid:




How do i implement this messaging?
Last edited on
Topic archived. No new replies allowed.