1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
#include<iostream>
#include<winsock.h>
#include<cstdio>
#include<cstdlib>
#include<string>
//#include"dos.h"
using namespace std;
#define NETWORK_ERROR -1
#define NETWORK_OK 0
int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hprevinstance,LPSTR lp,int n)
{
WSADATA ws;
int nret;
WSAStartup(0x0101,&ws);
SOCKET lsocket;
char servername[50];
cout<<"Enter Your Name : ";
gets(servername);
cout<<"\nCreating Socket.......";
/*
for(int i=0;i<5;i++)
{
delay(400);
cout<<".";
}
*/
lsocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(lsocket==INVALID_SOCKET)
{
cout<<"\nCould Not Creat Listening Socket.";
WSACleanup();
getchar();
return NETWORK_ERROR;
}
else
cout<<"Socket Created";
//Use a SOCKADDR_IN struct to fill addr info
SOCKADDR_IN serverinfo;
serverinfo.sin_family=AF_INET;
serverinfo.sin_addr.s_addr=inet_addr("127.0.0.1");
serverinfo.sin_port=htons(2609);
//Bind the socket to our local server addr
cout<<"\nBinding Socket..........";
/*
for(i=0;i<5;i++)
{
delay(400);
cout<<".";
}
*/
nret=bind(lsocket,(SOCKADDR *)&serverinfo,sizeof(struct sockaddr));
if(nret==SOCKET_ERROR)
{
cout<<"\nCould Not Bind Listening Socket";
WSACleanup();
getchar();
return NETWORK_ERROR;
}
else
cout<<"Socket Bound";
//Make the bound socket listen.Up to 10 ma wait at any one time
cout<<"\nPutting Bound Socket to Listening Mode....";
/*
for(i=0;i<5;i++)
{
delay(400);
cout<<".";
}
*/
nret=listen(lsocket,10);
if(nret==SOCKET_ERROR)
{
cout<<"\nCould Not Attain Listen Mode";
WSACleanup();
getchar();
return NETWORK_ERROR;
}
else
cout<<"Entered Listening Mode";
//Wait for client
cout<<"\nWaiting for Client .......";
SOCKET commsocket;
commsocket=accept(lsocket,NULL,NULL);
if(commsocket==INVALID_SOCKET)
{
cout<<"\nCould Not Setup Connection On Request";
WSACleanup();
getchar();
return NETWORK_ERROR;
}
else
{
cout<<"\nConnection Setup Successfull";
}
char sendbuffer[256]="\nThis is server,reporting on port 2609";
char recvbuffer[256];
while(1)
{
for(int i=0;i<255;i++)
{
sendbuffer[i]=0;
recvbuffer[i]=0;
}
cout<<"\n"<<servername<<" :";
gets(sendbuffer);
if(strcmpi(sendbuffer,"BYE")==0)
{
cout<<"\n\nSwitching off the chat and exiting";
WSACleanup();
getchar();
break;
}
else
{
nret=send(commsocket,sendbuffer,255,0);
if(nret==SOCKET_ERROR)
{
cout<<"\nUnable to send.Please Retry";
WSACleanup();
getchar();
}
else
{
nret=recv(commsocket,recvbuffer,255,0);
if(nret==SOCKET_ERROR)
{
cout<<"\nCould Not Hear Client";
WSACleanup();
getchar();
}
else
{
cout<<"\nClient : "<<recvbuffer;
}
}
}
}
cout<<"\n\nThank You For Using PG's Application.";
getchar();
return NETWORK_OK;
}
|