Trying to execute this Scoket program in VSCode for Windows. But getting this error. Please provide some solutions thanks
#include<stdio.h>
#include<stdio.h>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
int main(int argc , char *argv[])
{
WSADATA wsa;
SOCKET s , new_socket;
struct sockaddr_in server , client;
int c;
char *message;
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
printf("Initialised.\n");
//Create a socket
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
{
printf("Could not create socket : %d" , WSAGetLastError());
}
printf("Socket created.\n");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
//Bind
if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
{
printf("Bind failed with error code : %d" , WSAGetLastError());
exit(EXIT_FAILURE);
}
puts("Bind done");
//Listen to incoming connections
listen(s , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
while( (new_socket = accept(s , (struct sockaddr *)&client, &c)) != INVALID_SOCKET )
{
puts("Connection accepted");
//Reply to the client
message = "Hello Client , I have received your connection. But I have to go now, bye\n";
send(new_socket , message , strlen(message) , 0);
}
if (new_socket == INVALID_SOCKET)
{
printf("accept failed with error code : %d" , WSAGetLastError());
return 1;
}
closesocket(s);
WSACleanup();
return 0;
}
[Running] cd "c:\Users\test\Desktop\ClientServerSocket\Socket\" && g++ Client_Server.cpp -o Client_Server && "c:\Users\test\Desktop\ClientServerSocket\Socket\"Client_Server
Client_Server.cpp: In function 'int main(int, char**)':
Client_Server.cpp:62:13: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
message = "Hello Client , I have received your connection. But I have to go now, bye\n";
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0x37): undefined reference to `WSAStartup@8'
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0x48): undefined reference to `WSAGetLastError@0'
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0x8a): undefined reference to `socket@12'
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0xa0): undefined reference to `WSAGetLastError@0'
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0xdb): undefined reference to `htons@4'
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0x102): undefined reference to `bind@12'
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0x114): undefined reference to `WSAGetLastError@0'
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0x14f): undefined reference to `listen@8'
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0x187): undefined reference to `accept@12'
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0x1d4): undefined reference to `send@16'
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0x1e4): undefined reference to `WSAGetLastError@0'
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0x206): undefined reference to `closesocket@4'
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0x20e): undefined reference to `WSACleanup@0'
collect2.exe: error: ld returned 1 exit status