neocool
Member level 4
in_class_host
Hello,
I am trying to create a server and a client using c. Using Dev C++ 4.9.8.0 compiler, I cannot compile my code.
First of all, the examples I've found on internet use socket.h, while this version of compier does not have it, just winsock.h
So, I've changed the #include <winsock.h> to my file.
When I compile, it gives me errors like redefinition of "struct in_addr", INADDR_ANY, IN_CLASS_HOST, IN_CLASSA and others.. I don't know where it get's doubles from..
Any help would be appreciated.
Thanks
Here is my code:
--------------------------
#include <string.h>
#include <sys/types.h>
//#include <sys/socket.h>
#include <winsock.h>
#include <netinet/in.h>
#define MYPORT 1026 // connection port for ComBlock
#define BACKLOG 2 // how many pending connections queue will hold
main()
{
int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector's address information
int sin_size;
int lenof_buf = 8192; //max packet size = 8K
int bytes_received;
void char *buf =[];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
// error checking omitted
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
// error checking omitted
sin_size = sizeof(struct sockaddr_in);
bind(sockfd, (struct sockaddr *)&my_addr, &sin_size);
listen(sockfd, BACKLOG);
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
bytes_received = recv(new_fd, &buf, lenof_buf, 0);
printf("%c", buf);
}
---------------------------------------
Hello,
I am trying to create a server and a client using c. Using Dev C++ 4.9.8.0 compiler, I cannot compile my code.
First of all, the examples I've found on internet use socket.h, while this version of compier does not have it, just winsock.h
So, I've changed the #include <winsock.h> to my file.
When I compile, it gives me errors like redefinition of "struct in_addr", INADDR_ANY, IN_CLASS_HOST, IN_CLASSA and others.. I don't know where it get's doubles from..
Any help would be appreciated.
Thanks
Here is my code:
--------------------------
#include <string.h>
#include <sys/types.h>
//#include <sys/socket.h>
#include <winsock.h>
#include <netinet/in.h>
#define MYPORT 1026 // connection port for ComBlock
#define BACKLOG 2 // how many pending connections queue will hold
main()
{
int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector's address information
int sin_size;
int lenof_buf = 8192; //max packet size = 8K
int bytes_received;
void char *buf =[];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
// error checking omitted
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
// error checking omitted
sin_size = sizeof(struct sockaddr_in);
bind(sockfd, (struct sockaddr *)&my_addr, &sin_size);
listen(sockfd, BACKLOG);
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
bytes_received = recv(new_fd, &buf, lenof_buf, 0);
printf("%c", buf);
}
---------------------------------------