| | |
connecting client to server
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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
server-side
Client-side
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
#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
•
•
Join Date: Jan 2008
Posts: 119
Reputation:
Solved Threads: 10
•
•
•
•
yah...that's what my problem is...I cant send & receive...how can I?
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
![]() |
Similar Threads
- Connecting to SQL server DB from VB.NET app (VB.NET)
- server notify client to update MySQL table (C)
- Connecting to a SQL Server Database using VB (VB.NET)
- Cannot FTP with Windows XP with any client or to any server (Networking Hardware Configuration)
- Problem with Client-Server Socket Connection (Java)
- Is MySQL available for use as a local database? (MySQL)
- "Server busy" and adware when connecting to cstrike server (Viruses, Spyware and other Nasties)
- My mac won't connect with SMTP server (OS X)
Other Threads in the C++ Forum
- Previous Thread: Help? reading from one file to another...
- Next Thread: GetFullPathName() doesnot return the correct path
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





