hi, my problem is about socket programming. My code below only connects the client to the server, and it works just fine. However, I should create a 2player game which of course require the interaction of the 1st and 2nd player. The problem here is that they really don't interact with each other, it is as if the terminal for the client side is a separate program from the terminal for the server side(sorry if I can't explain it clearly..lol) yeah, the client connected successfully, but they don't interact..pls I really need this code or else I'll fail my subject..tnx a lot!

Client-side

#include <iostream>
#include <iomanip>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 
#include <stdio.h>
#include <termios.h>
#include <unistd.h>


using namespace std;

const int totchan=7;

void error(char *msg)
{
    perror(msg);
    exit(0);
}
 
int main(int argc, char *argv[])
{
    int sockfd, portno, n, input;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256];
    if (argc < 3) {
       fprintf(stderr,"usage %s hostname port\n", argv[0]);
       exit(0);
    }
    portno = atoi(argv[2]);
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
        error("ERROR opening socket");
    server = gethostbyname(argv[1]);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, 
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);
    serv_addr.sin_port = htons(portno);
    
    if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)  
        error("ERROR connecting");


cout<<"+--------------------------------------+"<<endl;
    cout<<"|   This is a shooting game. You must  |"<<endl;
    cout<<"|   type as many as you can, the       |"<<endl;
    cout<<"|   letters on the screen that you can |"<<endl;
    cout<<"|   see. The player with the most      |"<<endl;
    cout<<"|   number of points wins the game!    |"<<endl;
    cout<<"|   Each player only has 3 lives,      |"<<endl;
    cout<<"|   so if it runs out, you lose!       |"<<endl;
    cout<<"+--------------------------------------+"<<endl;                      
   
    string blocks[25][25];
    string player1="^^^";
    string player2="^^^";

//blah blah blah this is the main game

server-side

#include <iostream>
#include <iomanip>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 
#include <stdio.h>
#include <termios.h>
#include <unistd.h>


using namespace std;

const int totchan=7;

void error(char *msg)
{
    perror(msg);
    exit(1);
}



int main(int argc, char *argv[])
{
     int sockfd, newsockfd, portno, clilen;
     char buffer[256];
     struct sockaddr_in serv_addr, cli_addr;
     int n,input;
     if (argc < 2) {
         fprintf(stderr,"ERROR, no port provided\n");
         exit(1);
     }
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) 
        error("ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);
     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0) 
              error("ERROR on binding");
     listen(sockfd,5);
     clilen = sizeof(cli_addr);
     newsockfd = accept(sockfd, 
                 (struct sockaddr *) &cli_addr, (socklen_t *)
                 &clilen);
     if (newsockfd < 0) 
          error("ERROR on accept");


     cout<<"+--------------------------------------+"<<endl;
    cout<<"|   This is a shooting game. You must  |"<<endl;
    cout<<"|   type as many as you can, the       |"<<endl;
    cout<<"|   letters on the screen that you can |"<<endl;
    cout<<"|   see. The player with the most      |"<<endl;
    cout<<"|   number of points wins the game!    |"<<endl;
    cout<<"|   Each player only has 3 lives,      |"<<endl;
    cout<<"|   so if it runs out, you lose!       |"<<endl;
    cout<<"+--------------------------------------+"<<endl;                      
   
    string blocks[25][25];
    string player1="^^^";
    string player2="^^^";


//blah blah blah this is the main game

Recommended Answers

All 5 Replies

ok, what u posted is just the connection part.
What do you mean they don't interract? I mean, u know u have to send messages from server to client and from client to server via send, recv || write, read.

try posting the entire problem ... i mean your assignment requirements and how you thought about implementig it. Then I will probably be able to help you with it a bit more.

yah...that's what my problem is...I cant send & receive...how can I?

yah...that's what my problem is...I cant send & receive...how can I?

It works just fine for me. I just added the followng lines to your program:
client side:

char buff[16] = "mamaaremere";
send( sockfd, buff, 16, 0 );

server side:

char buff[16];
int howmany = recv( newsockfd, buff, 16, 0 );
cout<<howmany<<" "<<buff<<endl;

just as expected... the server prints the recived message: mamaaremere

hmmm..im almost there!!tnx!! do you know how to read & write???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.