Jump to content
  • Advertisement
Sign in to follow this  
James_Alex

sock_stream won't work !

This topic is 3253 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, i tries that
int Server::Start(int Port)
{
    int err;
    WSAStartup(MAKEWORD(2, 2), &inite);
    socke = socket(AF_INET,SOCK_STREAM,0);
	if(socke == INVALID_SOCKET)
    {
        Sleep(4000);
        exit(0);
        return 0;
    }

    i_socke.sin_family = AF_INET;
    i_socke.sin_addr.s_addr = htonl(INADDR_ANY);
    i_socke.sin_port = htons(Port);    
    err = bind(socke,(LPSOCKADDR)&i_socke,sizeof(i_socke));
    if(err != 0)
    {

        return 0;
    }
	int iOptVal = 1;
	int iOptLen = sizeof(iOptVal);
	err = setsockopt(socke, SOL_SOCKET, SO_KEEPALIVE, (char*)&iOptVal, iOptLen); 
    if(err != 0)
    {

        return 0;
    }
    err = listen(socke, 5);
    if(err != 0)
    {

        return 0;
    }
   	return 1;
}

int Server::Recive()
{

	char Buf[256];
	int reclen = sizeof(i_socke);
	int recl = recvfrom(socke, (char *)&Buf, sizeof(Buf), 0, (sockaddr *)&i_socke, &reclen);
	if(recl < 0)
	{
		
		return 0;		
	}
	printf("%s", Buf);


	return recl;
it's my server Code
int Client::Connect(char *IP, int Port)
{
   	sprintf_s(sIP, sizeof(sIP), "%s", IP);
	sPort = Port;
	int err;
	//chatbox->AddMessage("Connecting to %s:%d...\n", IP, Port);
	printf("Connecting to %s:%d...\n", IP, Port);
    WSAStartup(MAKEWORD(2, 2), &init);
    sock = socket(AF_INET,SOCK_STREAM,0);
    if(sock == INVALID_SOCKET)
    {
		ConnectionFailed(IP, Port, "Failed to connect to the server, Retrying...\n", IP, Port);
        return 1;
    }
    i_sock.sin_family = AF_INET;
    i_sock.sin_addr.s_addr = inet_addr(IP);
    i_sock.sin_port = htons(Port); 
    int ss = connect(sock, (struct sockaddr *)&i_sock, sizeof(i_sock));
	if(ss != 0)
    {
		ConnectionFailed(IP, Port, "Failed to connect to the server, Retrying...\n", IP, Port);
        return 1;
    }
	char servname[256];
	gethostname(servname, 256);
	printf("Succefully connected to %s.\n", servname);
	return 1;

}

int Client::Send(const char *Msg, int len)
{
   	int slen;
(sockaddr *)&i_sock, sizeof(i_sock));
	slen = sendto(sock, Msg, len, 0, (struct sockaddr *)&i_sock, sizeof(i_sock));
	if(slen < 0) 
	{
		ConnectionFailed(sIP, sPort, "A probleme has been occured when sending information, rectonnecting...\n"); 
		return 1;
	}
	DWORD nonBlocking = 1;
	if(ioctlsocket(sock, FIONBIO, &nonBlocking) != 0) 
	{ 
		ConnectionFailed(sIP, sPort, "A probleme has been occured when sending information, rectonnecting...\n"); 
		return 1;
	}
	printf("Informations has been succefully sended(%d bytes).\n", slen);
	return slen;
and this is my Client Code but the probleme that the Client always says "Informations has been succefully sended([len] bytes)" but nothing was recived in the server and when i use WSAGetLastError in the Client it gives me 0 but in the server it gives me 10057 "SOCKNOTCONN" http://msdn.microsoft.com/en-us/library/ms740668%28VS.85%29.aspx#winsock.wsaenotconn_2 and it comes from sendto() function http://msdn.microsoft.com/en-us/library/ms740148%28VS.85%29.asp but when i change to "socket(AF_INET, SOCK_DGRAM,0)" it works nice but i won't use datagram beacause it dosen't work with connect() function anyway is there any probleme in my code ? or must i add something ? thx

Share this post


Link to post
Share on other sites
Advertisement
sendto()/recvfrom() are meant to be used on datagram (mostly UDP) sockets. UDP is a connectionless protocol, and since you say you want connections, TCP is what you should be using. As such, use send()/recv() instead. The reason your server's receive call fails is that you are attempting to receive on a listening socket. A listening socket is not associated with a particular client; its only purpose is to accept incoming connections. Also, why are you using nonblocking sockets, and more importantly, why are you setting the nonblock flag whenever Client::Receive() is called?

Share this post


Link to post
Share on other sites
You have no call to accept() on your server. You need to check the listening socket for an incoming connection (typically done with select() ), and then call accept. The accept() call returns a new socket which is the connection to the client. Then you can read from that socket.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!