I'm using G++ in Ubuntu 11.04 and I face the following error in in compiling this simple client server communication program.
ahmad@ahmad-ThinkCentre-S51:~/jan$ g++ clint.cpp -o myclient.exe clint.cpp:11:20: fatal error: winsock.h: No such file or directory compilation terminated.
my code is:-
include<iostream>
include<conio.h>
include<winsock.h>
include<stdlib.h>
include<fstream>
using namespace std;
pragma comment(lib, "winmm.lib")
pragma comment(lib, "wsock32.lib")
pragma comment(lib, "advapi32.lib")
define NETWORK_ERROR -1 //define some constants
define NETWORK_OK 0 //for error handling
int main()
{
int nret; //for error handling
WSADATA WsaDat;
//The WSADATA structure contains information about the Windows Sockets implementation
WSAStartup(MAKEWORD(1,1),&WsaDat);
SOCKET mysocket;
mysocket = socket( AFINET, SOCKDGRAM, IPPROTO_UDP );
if( mysocket == INVALID_SOCKET )
{
cout<<"Socket is not created"<<endl;
WSACleanup(); // Shutdown Winsock
return NETWORK_ERROR; //NETWORK_ERROR is a constant, its value is -1
}
else
cout<<"Socket is created"<<endl;
SOCKADDR_IN serveraddr;
serveraddr.sinfamily=AFINET;
serveraddr.sin_port= htons(5555);
serveraddr.sinaddr.saddr= inet_addr("127.0.0.1"); //servers IP address
nret= connect (mysocket , (SOCKADDR*)(&serveraddr),sizeof(serveraddr) );
if (nret==SOCKET_ERROR)
{
cout<<"Cannot establish the connection"<<endl;< p="">
WSACleanup(); // Shutdown Winsock
return NETWORKERROR; //NETWORKERROR is a constant, its value is -1
}
else
cout<<"Connection established successfully"<<endl;< p="">
char msg[100];
char gg[100];
fstream file;
file.open("talal.txt", ios::out);
int i=0,k;
cout<<"Please enter the desired message into the file which is to be send to the server"<<endl;< p="">
cin.getline(msg,100);
file<<msg;< p="">
file.close();
file.open("talal.txt", ios::in);
file.getline(msg, 100);
k=strlen(msg);
for(i=0; i<k 2;="" i++)<="" p="">
{
gg[i]=msg[i];
msg[i]=msg[k-i-1];
msg[k-i-1]=gg[i];
}
//Sending of message
sendto(mysocket,msg,sizeof(msg),0,(SOCKADDR *)(& serveraddr),(sizeof serveraddr));
cout<<"your file has been sent to the server"<<endl;< p="">
closesocket(mysocket);
WSACleanup();
return 0;
} //end of program
0 answers
No answers yet.
Log in to answer this question.