hi, I am trying to establish a connection between two laptops, as a beginner encountered a problems that are still to be learnt, but still I would like to know.
I have two app, first is server on win wista:
#include "stdafx.h"
#include "winsock2.h"
#include <string>
#include <iostream>
//using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsaData;
SOCKET ListeningSocket;
SOCKET NewConnection;
SOCKADDR_IN ServerAddr;
SOCKADDR_IN ClientAddr;
int Port=6000;
WSAStartup(MAKEWORD(2,2),&wsaData);
ListeningSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
ServerAddr.sin_family=AF_INET;
ServerAddr.sin_port=htons(6000);
ServerAddr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
bind(ListeningSocket,(SOCKADDR*)&ServerAddr,sizeof(ServerAddr));
listen(ListeningSocket,5);
int nClient;
while (true) {
NewConnection=SOCKET_ERROR;
while (NewConnection==SOCKET_ERROR) {
NewConnection=accept(ListeningSocket,NULL,NULL);
}
std::cout<<"connected.."<<std::endl;
break;
}
//cout<<inet_ntoa(ClientAddr.sin_addr)<<endl;
return 0;
}
yes..I have two laptops with two diff os, and client is this:
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdlib.h>
/*
*
*/
int main(int argc, char** argv) {
int clientSocked;
struct sockaddr_in serverAddr;
serverAddr.sin_family=AF_INET;
serverAddr.sin_addr.s_addr=htonl(INADDR_ANY);//inet_addr("my ip");
serverAddr.sin_port=htons(6000);
if ((clientSocked=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))<0)
return (EXIT_FAILURE);
if ((connect(clientSocked,(struct sockaddr*)&serverAddr,sizeof(serverAddr)))<0)
return (EXIT_FAILURE);
return (EXIT_SUCCESS);
}
this app doesn't work and i dont really know why. I assume i has something to do with the ip i am giving. I get the ip from internet and the ip is the same for both laptops..
this app doesn't work and i dont really know why. I assume i has something to do with the ip i am giving. I get the ip from internet and the ip is the same for both laptops..
And also to set an address for stuct sockadd_in address i need to use somehow different internal structure sockaddr_in.sin_addr.S_un.S_addr..
I am reading book 2windows networking" and this book gives me a diff internal structure sockadd_in.sin_addr.s_addr why is it changed?