• C++ Sockets Help
    10 replies, posted
I'm attempting to create a small project with C++, but I'm not very knowledgeable in the language. So can anyone help me out by linking or posting a sockets script for it.
If you know next to nothing about C++, you seriously don't want to dive in to using sockets. You will get nowhere. But, if it's Windows that you're talking about, then here's a Winsock video tutorial for you: [URL]http://www.youtube.com/watch?v=w5MPIkveJoQ[/URL]
I have used sockets in the past and I'm pretty good at catching on, I have a client ready in a different language and just need to make the server.
Here's the code that the Berkeley Sockets wikipedia page posts. Big fan of BSD sockets and natively supported by all OS's. [cpp] /* Server code in C */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(void) { struct sockaddr_in stSockAddr; int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if(-1 == SocketFD) { perror("can not create socket"); exit(EXIT_FAILURE); } memset(&stSockAddr, 0, sizeof(struct sockaddr_in)); stSockAddr.sin_family = AF_INET; stSockAddr.sin_port = htons(1100); stSockAddr.sin_addr.s_addr = INADDR_ANY; if(-1 == bind(SocketFD,(const struct sockaddr *)&stSockAddr, sizeof(struct sockaddr_in))) { perror("error bind failed"); close(SocketFD); exit(EXIT_FAILURE); } if(-1 == listen(SocketFD, 10)) { perror("error listen failed"); close(SocketFD); exit(EXIT_FAILURE); } for(;;) { int ConnectFD = accept(SocketFD, NULL, NULL); if(0 > ConnectFD) { perror("error accept failed"); close(SocketFD); exit(EXIT_FAILURE); } /* perform read write operations ... */ shutdown(ConnectFD, SHUT_RDWR); close(ConnectFD); } return 0; } [/cpp] Keep in mind that this server can only handle one connection at a time.
for(;;)? Why not while()?
<Anticipating an onslaught of whining about me posting C code> [editline]11:09AM[/editline] [QUOTE=Eleventeen;18079765]for(;;)? Why not while()?[/QUOTE] No particular reason. A while loop is just a special case of a for loop. Many programmers do it this way.
Since that is C code will it work with C++?
[QUOTE=Lappy;18080008]Since that is C code will it work with C++?[/QUOTE] You should probably just give up now. But to answer your question, yes.
[QUOTE=Cathbadh;18079540]Big fan of BSD sockets and natively supported by all OS's.[/QUOTE] WinSock is only based on BSD sockets, it's not directly compatible and as such Windows does not "natively support" BSD sockets. Your code snippet wouldn't work by a long shot, for example.
[QUOTE=jA_cOp;18080200]WinSock is only based on BSD sockets, it's not directly compatible and as such Windows does not "natively support" BSD sockets. Your code snippet wouldn't work by a long shot, for example.[/QUOTE] The only thing that wouldn't work is the posix parts, like perror() and close(), and those have Windows equivalents. I'm not sure what you mean by "wouldn't work by a long shot" perror() is just for error reporting and is an acceptable loss (but you should still detect and report errors). close() can just be replaced by closesocket().
[QUOTE=Cathbadh;18080279]The only thing that wouldn't work is the posix parts, like perror() and close(), and those have Windows equivalents. I'm not sure what you mean by "wouldn't work by a long shot" perror() is just for error reporting and is an acceptable loss (but you should still detect and report errors). close() can just be replaced by closesocket().[/QUOTE] There's also WSAStartup and WSACleanup, and the includes are different. There's more, but I can't remember right now, haven't used WinSock in a while.
Sorry, you need to Log In to post a reply to this thread.