Hi, I find sockets quite interesting and since I just have started to learn a little about them i'm not a pro
(that won't be a secret to anyone who see my code.)
What I want to know is, if there's alot difference between unix socket programming and winsock.
Here's my unix code for a server program which does nothing except telling you when an incoming connection is accepted.
Any coments on the code is very welcome, I would really appreciateCode:#include <iostream.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> int main() { int serv, clnt, port, sin_size = sizeof(sockaddr_in); sockaddr_in server, client; bool loop = true; cout << "Select port: "; cin >> port; server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons(port); if ((serv = socket(AF_INET, SOCK_STREAM, 0)) < 0) { cout << "An error occured while trying to create socket... quitting!"; return 1; } if (bind(serv, (sockaddr*)&server, sizeof(sockaddr)) < 0) { cout << "An error occured while trying to bind socket... quitting!"; return 1; } if (listen(serv, 1) < 0) { cout << "An error occured while trying to listen for a connection... quitting!"; return 1; } while(loop) { if (clnt = accept(serv, (sockaddr*)&client, &sin_size) < 0) { cout << "Could not accept connection!"; return 1; } cout << "Got connection from: " << inet_ntoa(client.sin_addr) << "!"; if (send(clnt, "Connection successfull.\n", 24, 0) > 0) { loop = false; } close(clnt); } return 0; }
any corrections if there's something wrong or stupid about the code.
Most importantly I would really really appreciate a good link for a winsock tutorial.



LinkBack URL
About LinkBacks




a really friendly OS for a programmer!