I'm fairly new to C, but i'm eager to get into programming with sockets in it. I decided to download a source that creates a simple listening socket so i can get an idea of how it works.. but I haven't been able to compile it successfully.
I'm pretty sure i'm just doing something wrong.. but here it is:
Start dev-c++, File -> New -> Project. Select console application, select it as a C Project, then OK. Then pasted the following code into main.c and compiled.
#include <winsock.h> // winsock.h always needs to be included
#include <stdio.h>
int main(int argc, char** argv) {
WORD version = MAKEWORD(1,1);
WSADATA wsaData;
int nRet;
WSAStartup(version, &wsaData);
SOCKET listeningSocket;
listeningSocket = socket(AF_INET, // Go over TCP/IP
SOCK_STREAM, // Socket type
IPPROTO_TCP); // Protocol
if (listeningSocket == INVALID_SOCKET) {
printf("Error at socket()");
WSACleanup();
return 0;
}
SOCKADDR_IN saServer;
saServer.sin_family = AF_INET;
saServer.sin_addr.s_addr = INADDR_ANY; // Since this is a server, any address will do
saServer.sin_port = htons(8888); // Convert int 8888 to a value for the port field
nRet = bind(listeningSocket, (LPSOCKADDR)&saServer, sizeof(struct sockaddr));
if (nRet == SOCKET_ERROR) {
printf("Error at bind()");
WSACleanup();
return 0;
}
nRet = listen(listeningSocket, 10); // 10 is the number of clients that can be queued
if (nRet == SOCKET_ERROR) {
printf("Error at listen()");
WSACleanup();
return 0;
}
SOCKET theClient;
theClient = accept(listeningSocket,
NULL, // Address of a sockaddr structure (see below)
NULL); // Address of a variable containing the size of sockaddr
if (theClient == INVALID_SOCKET) {
printf("Error at accept()");
WSACleanup();
return 0;
}
closesocket(theClient);
closesocket(listeningSocket);
WSACleanup();
}
After attempting to compile it I get the following error messages:
C:\Dev-Cpp\Templates\main.c
[Warning] In function `main':
20 C:\Dev-Cpp\Templates\main.c
parse error before `listeningSocket'
22 C:\Dev-Cpp\Templates\main.c
`listeningSocket' undeclared (first use in this function)
(Each undeclared identifier is reported only once
for each function it appears in.)
35 C:\Dev-Cpp\Templates\main.c
parse error before `saServer'
37 C:\Dev-Cpp\Templates\main.c
`saServer' undeclared (first use in this function)
67 C:\Dev-Cpp\Templates\main.c
parse error before `theClient'
69 C:\Dev-Cpp\Templates\main.c
`theClient' undeclared (first use in this function)
Could someone enlighten me on what I am doing wrong? thanks.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello,
I'm fairly new to C, but i'm eager to get into programming with sockets in it. I decided to download a source that creates a simple listening socket so i can get an idea of how it works.. but I haven't been able to compile it successfully.
I'm pretty sure i'm just doing something wrong.. but here it is:
Start dev-c++, File -> New -> Project. Select console application, select it as a C Project, then OK. Then pasted the following code into main.c and compiled.
#include <winsock.h> // winsock.h always needs to be included
#include <stdio.h>
int main(int argc, char** argv) {
WORD version = MAKEWORD(1,1);
WSADATA wsaData;
int nRet;
WSAStartup(version, &wsaData);
SOCKET listeningSocket;
listeningSocket = socket(AF_INET, // Go over TCP/IP
SOCK_STREAM, // Socket type
IPPROTO_TCP); // Protocol
if (listeningSocket == INVALID_SOCKET) {
printf("Error at socket()");
WSACleanup();
return 0;
}
SOCKADDR_IN saServer;
saServer.sin_family = AF_INET;
saServer.sin_addr.s_addr = INADDR_ANY; // Since this is a server, any address will do
saServer.sin_port = htons(8888); // Convert int 8888 to a value for the port field
nRet = bind(listeningSocket, (LPSOCKADDR)&saServer, sizeof(struct sockaddr));
if (nRet == SOCKET_ERROR) {
printf("Error at bind()");
WSACleanup();
return 0;
}
nRet = listen(listeningSocket, 10); // 10 is the number of clients that can be queued
if (nRet == SOCKET_ERROR) {
printf("Error at listen()");
WSACleanup();
return 0;
}
SOCKET theClient;
theClient = accept(listeningSocket,
NULL, // Address of a sockaddr structure (see below)
NULL); // Address of a variable containing the size of sockaddr
if (theClient == INVALID_SOCKET) {
printf("Error at accept()");
WSACleanup();
return 0;
}
closesocket(theClient);
closesocket(listeningSocket);
WSACleanup();
}
After attempting to compile it I get the following error messages:
C:\Dev-Cpp\Templates\main.c
[Warning] In function `main':
20 C:\Dev-Cpp\Templates\main.c
parse error before `listeningSocket'
22 C:\Dev-Cpp\Templates\main.c
`listeningSocket' undeclared (first use in this function)
(Each undeclared identifier is reported only once
for each function it appears in.)
35 C:\Dev-Cpp\Templates\main.c
parse error before `saServer'
37 C:\Dev-Cpp\Templates\main.c
`saServer' undeclared (first use in this function)
67 C:\Dev-Cpp\Templates\main.c
parse error before `theClient'
69 C:\Dev-Cpp\Templates\main.c
`theClient' undeclared (first use in this function)
Could someone enlighten me on what I am doing wrong? thanks.