All pastes #625826 Raw Edit

Untitled

public text v1 · immutable
·published 2007-07-19 06:14 UTC
rendered paste body
#include <iostream>
#include <winsock.h>
#include <winsock2.h>
using namespace std; 
int server(){
#define BUF_LEN 512
SOCKET listen1 , client;
WSAData wsadat;
int result;
char recvbuf[BUF_LEN];
int recvbuflen = BUF_LEN;
int error;
int sendresult;

char local[100] = "0.0.0.0"; 

result = WSAStartup(MAKEWORD(2,2), &wsadat);// initiate winsock with winsock version 2.2
if(result != 0){
printf("WSAStartup() failed: %d\n", result);
goto clean; // if result == 0 b.c WSAStartup if success is 1 
}
SOCKADDR_IN server; //struct for Host info
server.sin_family = AF_INET; //AF_INET is internet something 
server.sin_port = htons(2007); //port but htons converts to byte order on 2007
server.sin_addr.s_addr = inet_addr(local); // inet addr converts to
//net work byte order from local variable which is 127.0.0.1


listen1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //listen1 
// is a socket but w.e so listen 1 is socket infor like internet 
//family then SOCK_STREAM, which is for beginners 
// and ipproto_TCP is so you use tcp instead of UDP
if(listen1 == INVALID_SOCKET){
error = WSAGetLastError();
cout<<"socket() returned error: "<<error<<endl;
goto clean;
//is for error checking WSAGetLastError() returns error code
//goto MSDN search WSAGetLastError(); and it will show error code list

}


result = bind(listen1,(SOCKADDR*)&server, sizeof(server));
//binds socket to local address, kinda confusing
// just MSDN this b.c it's hard for me to explain but ill try
//bind(SOCKET, (SOCKADDR*)&SOCKADDR_IN VAR, sizeof(SOCKADDR_IN VAR);
//or in mine
//bind(listen1, (SOCKADDR*)&server, sizeof(server));
if(result == SOCKET_ERROR){
error = WSAGetLastError();
cout<<"bind() returned error: "<<error<<endl;
goto clean;
//again error checking
}

result = listen(listen1, SOMAXCONN);
//listens for connections.
// listen(listen1 is socket, SOMAXCONN is a param that determines
//how many connections can be accepted i think from your internet speed
if(result == SOCKET_ERROR){
error = WSAGetLastError();
cout<<"listen() returned error: "<<error<<endl;
goto clean;
}

client = accept(listen1, NULL,NULL);
//client is another socket but accept will actually 
//accept a connection after it's been listened now MSDN accept();
//b.c idk what the NULL,NULL does it's really confusing
if(client == SOCKET_ERROR){
error = WSAGetLastError();
cout<<"accept() returned error: "<<error<<endl;
goto clean;
}
closesocket(listen1);
//closes the socket no longer needed.
cout<<"what would you like to send: "<<endl;
cin>>recvbuf;
do{
result =recv(client, recvbuf, recvbuflen, MSG_PEEK);
//only reason we put result =recv() is for error checking
//now recv(SOCKEt, incoming buf, Len for incoming buf, flags 
//MSG_PEEKS at the data and shows you but dosen't remove from 
//input queue which i think is a problem.
if(result > 0){
printf(" Bytes Recived: %d\n", result);
cout<<recvbuf<<endl;
sendresult = send(client, recvbuf, result, 0);
//send(SOCKET, Buf for outgoing, bytes is result, 0 for no flags.
if(sendresult == SOCKET_ERROR){
error = WSAGetLastError();
cout<<"send() returned error: "<<error<<endl;
goto clean;

}printf("Bytes Sent: %d\n", sendresult);}
else if(result ==0) printf("connection closed...\n");
else{
printf("Recived failed: %d\n", WSAGetLastError());
}}while(result>0);

clean:
WSACleanup();//WSACleanup() cleans up winsock shit.. idk put
// at end always no params
closesocket(listen1);
system("PAUSE");
}
int main(){
server();
}