| | |
socket programming
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
this code is a game that uses socket to be able to connect to each other so that there would be a 2player game.. but there something wrong here...here's the code..i'm using linux as my operating system ..this is the client side..
this is the server side..
you know what people...my professor didn't taught us socket..he simply game us this project..so any help would be appreciated..tnx
C++ Syntax (Toggle Plain Text)
#include <stdio.h> #include <iostream> //#include <conio.h> #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> using namespace std; void error(char *msg) { perror(msg); exit(0); } int main(int argc, char *argv[]) { int sockfd,portno, n, n1; 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,&serv_addr,sizeof(serv_addr)) < 0) // error("ERROR connecting"); //if (connect(sockfd, &serv_addr,sizeof(serv_addr)) < 0) //error("ERROR connecting"); printf("Welcome to Battle of Jack 'en Poy\n"); printf("Please enter your name: "); bzero(buffer,256); fgets(buffer,255,stdin); n = write(sockfd,buffer,strlen(buffer)); bzero(buffer,256); n = write(sockfd," ! ",3); if (n < 0) error("ERROR writing to socket"); bzero(buffer,256); n = read(sockfd,buffer,255); if (n < 0) error("ERROR reading from socket"); printf("%s\n",buffer); return 0; }
this is the server side..
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> #include <iomanip> #include <string> #include <time.h> #include <windows.h> #include <conio.h> using namespace std; #define splayer 1 #define cplayer 0 char buffer_name[256]; int newsockfd; int n; void error(char *msg) { perror(msg); exit(1); } void game(); string player1="^^^"; string player2="^^^"; int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, clilen, pid; struct sockaddr_in serv_addr, cli_addr; 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); while (1) { if (newsockfd < 0) error("ERROR on accept"); pid = fork(); if (pid < 0) error("ERROR on fork"); if (pid == 0) { close(sockfd); dostuff(); exit(0); } else close(newsockfd); } return 0; //game(); } void game() { 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 BLAH BLAH BLAH BLAH //THIS IS THE MAIN GAME
Are you being able to communicate from client to server ? what errors are u getting ?
![]() |
Similar Threads
- Socket Programming... (C#)
- About socket programming in php (PHP)
- Socket Programming (C++)
- Eclipse SDK/Socket Programming help (Java)
- Socket Programming (ASP.NET)
- C++ socket programming (C++)
- Socket programming + porting Unix to Win32 (C)
Other Threads in the C++ Forum
- Previous Thread: print what a pointer points to in an arry
- Next Thread: Easy help: Start and stop reading in the middle of a file
| Thread Tools | Search this Thread |
api application array arrays beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive return sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






