linking errors : undefined reference to `__imp_socket'
I have a simple client-server program in C, I am trying to compile it using GCC (the latest version) on windows10 but I am getting some linking errors like
undefined reference to `__imp_socket'
undefined reference to `__imp_htons'
undefined reference to `__imp_bind'
undefined reference to `__imp_listen'
undefined reference to `__imp_accept'
undefined reference to `__imp_gethostbyname'
undefined reference to `__imp_connect'
I've tried adding -lws2_32 with no luck; i.e. it still can't find the references. Can anyone else think of anything else I might be missing?
server
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <winsock2.h>
#include <windows.h>
// system file contains definations of system calls
// include<sys/socket.h>
#include <winsock.h>
//#inlude<netinet/in.h>
#include <ws2tcpip.h>
#include <ws2ipdef.h>
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
fprintf(stderr, "Port number not provided. program terminated\n");
exit(1);
}
int sockfd, newsockfd, portno, n;
char buffer[255];
struct sockaddr_in serv_addr, cli_addr;
socklen_t clilen;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
error("Error opening Socket.");
}
// memset((char *)&serv_addr, sizeof(serv_addr));
memset((char *)&serv_addr, 0, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.S_un.S_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
error("Binding Failed.");
}
listen(sockfd, 5);
// 5 is the number of cilents that can connect at the moment
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
if (newsockfd < 0)
{
error("Error while accepting");
}
while (1)
{
memset(buffer, 0, 256);
n = read(newsockfd, buffer, 255);
if (n < 0)
{
error("Error while reading the message");
}
printf("Client : %s\n", buffer);
memset(buffer, 0, 255);
fgets(buffer, 255, stdin);
n = write(newsockfd, buffer, strlen(buffer));
if (n < 0)
{
error("Error while writing");
}
int i = strncmp("Bye", buffer, 3);
if (i == 0)
{
break;
}
}
close(newsockfd);
close(sockfd);
return 0;
}
client
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
// read write
#include <sys/types.h>
#include <winsock2.h>
#include <windows.h>
// system file contains definations of system calls
// include<sys/socket.h>
#include <winsock.h>
//#inlude<netinet/in.h>
#include <ws2tcpip.h>
#include <ws2ipdef.h>
// #include <netdb.h>
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[255];
if (argc < 3)
// if less than 3 host has not provided port no.
{
fprintf(stderr, "usage %s hostname port\n", argv[0]);
exit(1);
}
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");
}
memset((char *)&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
memcpy((char *)server->h_addr_list, (char *)&serv_addr.sin_addr.S_un.S_addr, server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
error("Connection failed");
}
while (1)
{
memset(buffer, 0, 255);
fgets(buffer, 255, stdin);
n = write(sockfd, buffer, strlen(buffer));
if (n < 0)
{
error("Error while writing");
}
memset(buffer, 0, 255);
n = read(sockfd, buffer, 255);
if (n < 0)
{
error("Error while reading");
}
printf("Server: %s", buffer);
int i = strncmp("Bye", buffer, 3);
if (i == 0)
{
break;
}
}
close(sockfd);
return 0;
}
from Recent Questions - Stack Overflow https://ift.tt/Z0th7nA
https://ift.tt/XonDdbU
Comments
Post a Comment