WinSock issue
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: WinSock issue

  1. #1
    Join Date
    Nov 2010
    Location
    United Kingdom
    Posts
    11

    WinSock issue

    Hey guys, I was just wondering if anyone could help me out with WinSock, I've done all the reading up and stuff and I kind of started a project and am now looking to put it on the internet, I'm not looking to make a chat program but have started another program to test the send and recieve, now it works over "127.0.0.1" but does not work over a network and have been having trouble with it for a while now so any help would be greatly appreciated thank you

    Server side code:
    Code:
    CChatServer::CChatServer()
    {
        cout << "Starting up TCP Chat server\n";
    	m_bIsConnected = false;
    
        WSADATA wsaData;
    
        sockaddr_in local;
    
        int wsaret=WSAStartup(MAKEWORD(2,2),&wsaData);
    
        if(wsaret!=0)
        {
            return;
        }
    
        local.sin_family=AF_INET; 
        local.sin_addr.s_addr=INADDR_ANY; 
        local.sin_port=htons((u_short)8084); 
    
        m_SListenClient=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    
    
        if(m_SListenClient==INVALID_SOCKET)
        {
            return;
        }
    
    
        if(bind(m_SListenClient,(sockaddr*)&local,sizeof(local))!=0)
        {
            return;
        }
    
    
        if(listen(m_SListenClient,10)!=0)
        {
            return;
        }
    
    	m_bIsConnected = true;
        return;
    }
    
    CChatServer::~CChatServer()
    {
        closesocket(m_SListenClient);
    
        WSACleanup();
    }
    
    void CChatServer::StartListenClient()
    {
    
        sockaddr_in from;
        int fromlen=sizeof(from);
    
        m_SClient=accept(m_SListenClient,
             (struct sockaddr*)&from,&fromlen);
    
    	if(m_SClient != INVALID_SOCKET)
    		m_vClientList.push_back(m_SClient);
    
    	//AfxBeginThread(ServerRecThread,(void *)m_SClient);
    
    }
    
    
    
    int CChatServer::SendMessagePort(string sMessage)
    {
    		int iStat = 0;
    		list<SOCKET>::iterator itl;
    
    		if(m_vClientList.size() == 0)
    			return 0;
    
    		for(itl = m_vClientList.begin();itl != m_vClientList.end();itl++)
    		{
    			iStat = send(*itl,sMessage.c_str(),sMessage.size()+1,0);			
    			if(iStat == -1)
    				m_vClientList.remove(*itl);
    		}
    
    		if(iStat == -1)
    			return 1;
    
    		return 0;
    
    }
    
    int CChatServer::RecClient(SOCKET sRecSocket)
    {
        char temp[4096];
    	int iStat;
    		
        //cout <<inet_ntoa(from.sin_addr) <<":"<<temp<<"\r\n";
    		iStat = recv(sRecSocket,temp,4096,0);
    		if(iStat == -1)
    		{
    			m_vClientList.remove(sRecSocket);
    			return 1;
    		}
    		else
    		{
    			cout <<":"<<temp<<"\n";
    			//SendMessagePort(temp);
    			return 0;
    		}
    	return 0;
    
    }

    Client side code:
    Code:
    CIPMessage::CIPMessage()
    {
    	m_bIsConnected = false;
    }
    
    void CIPMessage::Init(string sIpAddress, int iPort)
    {
    
    	m_sServerIPAddress = sIpAddress;
    	m_iServerPort = iPort;
    	struct hostent *hp;
    	unsigned int addr;
    	struct sockaddr_in server;
    	
    
    	WSADATA wsaData;
    
    	int wsaret=WSAStartup(MAKEWORD(2,2),&wsaData);
    
    
    	if(wsaret!=0)
    	{
    		return;
    	}
    
    	conn = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    	if(conn==INVALID_SOCKET)
    		return;
    
    	addr = inet_addr(m_sServerIPAddress.c_str());
    	hp = gethostbyaddr((char*)&addr, sizeof(addr), AF_INET);
    	
    	if(hp==NULL)
    	{
    		closesocket(conn);
    		return;
    	}
    
    	server.sin_addr.s_addr=*((unsigned long*)hp->h_addr);
    	server.sin_family=AF_INET;
    	server.sin_port=htons(m_iServerPort);
    
    	if(connect(conn,(struct sockaddr*)&server,sizeof(server)))
    	{
    		closesocket(conn);
    		return;	
    	}
    	m_bIsConnected = true;
    	return;
    }
    
    CIPMessage::~CIPMessage()
    {
    	if(m_bIsConnected)
    		closesocket(conn);
    }
    
    int CIPMessage::SendMessagePort(string sMessage)
    {
    		int iStat = 0;
    
    		iStat = send(conn,sMessage.c_str(),sMessage.size()+1,0);
    		if(iStat == -1)
    			return 1;
    
    		return 0;
    
    }
    
    int CIPMessage::RecMessagePort()
    {
    
    		char acRetData[4096];
    		int iStat = 0;
    
    		iStat = recv(conn,acRetData,4096,0);
    		if(iStat == -1)
    			return 1;
    		cout<<"-->"<<acRetData<<"\n";
    
    		return 0;
    
    }
    If anyone can help I would be really thankfull

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: WinSock issue

    Quote Originally Posted by joro550 View Post
    ... now it works over "127.0.0.1" but does not work over a network and have been having trouble with it for a while now
    In what part(s) do you have trouble?
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2010
    Location
    United Kingdom
    Posts
    11

    Re: WinSock issue

    Quote Originally Posted by VictorN View Post
    In what part(s) do you have trouble?
    The only problem that I have is that the client won't connect to my I.P. address over a network

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: WinSock issue

    Do you mean that connect returns SOCKET_ERROR?
    Then what does WSAGetLastError return in this case?
    Victor Nijegorodov

  5. #5
    Join Date
    Nov 2010
    Location
    United Kingdom
    Posts
    11

    Re: WinSock issue

    Quote Originally Posted by VictorN View Post
    Do you mean that connect returns SOCKET_ERROR?
    Then what does WSAGetLastError return in this case?
    Yeah, I think that's right I tried it out with my friend who was on the same network and I used two i.p addresses one from cmd/ipconfig (which I think is my local ip address) and one fron a site http://www.whatsmyip.org/ (which is the i.p I use to connect to the internet i think)

    and the error which the cmd/ipconfig was 10060
    and the on with the http://www.whatsmyip.org/ was 10061

    I don't know if that helps?

    These were the error codes i got from WSAGetLastError() at the connect stage

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: WinSock issue

    Socket Error 10060 - Operation Timed Out
    Socket Error 10061
    http://www.google.com/search?q=iskindof&rls=com.microsoft:en-US:{referrer:source?}&ie=UTF-8&oe=UTF-8&sourceid=ie7#sclient=psy&hl=en&newwindow=1&rls=com.microsoft:en-US&#37;3A%7Breferrer%3Asource%3F%7D&q=10060+10061+socket+error&aq=f&aqi=&aql=&oq=&gs_rfai=&pbx=1&fp=5831956345d34357
    Victor Nijegorodov

  7. #7
    Join Date
    Nov 2010
    Location
    United Kingdom
    Posts
    11

    Re: WinSock issue

    Quote Originally Posted by VictorN View Post
    Socket Error 10060 - Operation Timed Out
    Socket Error 10061
    http://www.google.com/search?q=iskindof&rls=com.microsoft:en-US:{referrer:source?}&ie=UTF-8&oe=UTF-8&sourceid=ie7#sclient=psy&hl=en&newwindow=1&rls=com.microsoft:en-US&#37;3A%7Breferrer%3Asource%3F%7D&q=10060+10061+socket+error&aq=f&aqi=&aql=&oq=&gs_rfai=&pbx=1&fp=5831956345d34357
    Righto going to answer these one by one

    Time out - nothing much I can do about that one really
    The attempt to connect was forcefully rejected - not much I cna do I don't think?

    This means that the computer which VPOP3 was trying to connect to (probably at your ISP) refused the connection from the computer running VPOP3 - I don't know I dout I was running VPOP3

    This can either mean that the ISP server you specified was not running a mail service on the appropriate 'Port', that your ISP is having problems with their servers, or that your ISP's mail servers are too busy to accept new connections - Well the server was running

    It can also happen if a firewall (software or hardware) is blocking the connection attempt- turned off both mine and my friends for the test

    and I still got the same error codes, is it actually possible to connect it to the internet?

    Two things I haven't tried that I have just found are apparently Anti-virus software can interfere and I haven't tried port forwarding (but I'm not really in a position where I can try that)


    Read more: http://wiki.answers.com/Q/What_is_a_...#ixzz168gVGuwF
    Last edited by joro550; November 23rd, 2010 at 02:27 PM.

  8. #8
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    Re: WinSock issue

    If it works on a local network but not over internet it's probably a router/firewall that's blocking you.

    Even though it's not the exact thing you try to do here's a good explanation regarding the router/firewall dilemma http://slacksite.com/other/ftp.html
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  9. #9
    Join Date
    Nov 2010
    Location
    United Kingdom
    Posts
    11

    Re: WinSock issue

    Quote Originally Posted by S_M_A View Post
    If it works on a local network but not over internet it's probably a router/firewall that's blocking you.

    Even though it's not the exact thing you try to do here's a good explanation regarding the router/firewall dilemma http://slacksite.com/other/ftp.html
    Well that's the thing I don't think it does work over a local network (granted I'm not really in a postition to say this because my router set-up is kind of wierd) but it only works over the 127.0.0.1 ip address.

  10. #10
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    Re: WinSock issue

    If you can't try it without routers/firewalls you could try to run one (or both) side(s) in a virtual machine(s). That way you at least have control over the routing part (the machine running the virtual machines).
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center