C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 06-09-2007, 04:15 PM   #1
ex-SPEEDCUBER
 
Join Date: Dec 2005
Location: Canada
Posts: 265
Thumbs up (NVM SOLVED) not recving/sending and errno = 0

This is my first time doing any sort of network programming
I have both a server and a client that can connect to each other, the client can send data to the server but not vice versa

server:
Code:
void Init_Network()
{
    if (WSAStartup(MAKEWORD(2,2), &WSAData) == SOCKET_ERROR)
        MessageBox(0, "ERROR", "ERROR", MB_OK);

    SockFD = socket(PF_INET, SOCK_STREAM, 0);
    if (SockFD == SOCKET_ERROR)
        MessageBox(0, "ERROR", "ERROR", MB_OK);

    memset(My_Addr.sin_zero, '\0', sizeof My_Addr.sin_zero);
    My_Addr.sin_family = AF_INET;
    My_Addr.sin_port = htons(Port_Number);
    My_Addr.sin_addr.s_addr = INADDR_ANY;

    if (bind(SockFD, (struct sockaddr *)&My_Addr, sizeof(struct sockaddr)) == SOCKET_ERROR)
        MessageBox(0, "ERROR", "ERROR", MB_OK);

    if (listen(SockFD, 5) == SOCKET_ERROR)
        MessageBox(0, "ERROR", "ERROR", MB_OK);
}
Code:
                //send
                OutMessage[0] = 'c';
                OutMessage[1] = MyPos.X;
                OutMessage[2] = MyPos.Y;
                OutMessage[3] = '\0';
                nBytes = send(SockFD, OutMessage, 20, 0);
                std::cout<<errno;
                memset(OutMessage, ' ', 20);
client:
Code:
void Init_Network()
{
    if (WSAStartup(MAKEWORD(2,2), &WSAData) == SOCKET_ERROR)
        MessageBox(0, "ERROR", "ERROR", MB_OK);

    SockFD = socket(PF_INET, SOCK_STREAM, 0);
    if (SockFD == SOCKET_ERROR)
        MessageBox(0, "ERROR", "ERROR", MB_OK);

    Host_Entry = gethostbyname(IP);
    if (Host_Entry == NULL)
        MessageBox(0, "ERROR", "ERROR", MB_OK);

    Server_Addr.sin_family = AF_INET;
    Server_Addr.sin_port = htons(Port_Number);
    Server_Addr.sin_addr.s_addr = *(unsigned long*)Host_Entry->h_addr;
}
Code:
                //recv
                EnemyPrevPos.X = EnemyPos.X;
                EnemyPrevPos.Y = EnemyPos.Y;
                error = recv(ServerFD, InMessage, 20, 0);
                if (error == 0)
                {
                    std::cout<<"Your opponent has left the game."<<std::endl;
                    return 0;
                }
                if (error == -1)
                {
                    char temps[50];
                    SetConsoleCursorPosition(hOut, tempC);
                    perror(temps);
                    std::cout<<temps<<std::endl;
                    std::cout<<errno<<std::endl;
                    //return 0;
                }
                switch (InMessage[0])
                {
                    case 'c':
                        EnemyPos.X = InMessage[1];
                        EnemyPos.Y = InMessage[2];
                        break;
                    default:
                        SetConsoleCursorPosition(hOut, tempC);
                        std::cout<<InMessage;
                        break;
                }
                memset(InMessage, ' ', sizeof InMessage);
__________________
Please let me know if there's any part of my code I can change for optimization


OS: Windows XP and Ubuntu
IDE: wxDev-C++ and CodeBlocks
Compiler: GNU GCC

Last edited by h_howee; 06-09-2007 at 04:45 PM. Reason: misspelled [/code]
h_howee is offline   Reply With Quote
Old 06-09-2007, 04:43 PM   #2
ex-SPEEDCUBER
 
Join Date: Dec 2005
Location: Canada
Posts: 265
oops, i passed the wrong FDs to the send and recv function...
__________________
Please let me know if there's any part of my code I can change for optimization


OS: Windows XP and Ubuntu
IDE: wxDev-C++ and CodeBlocks
Compiler: GNU GCC
h_howee is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
errno function, error TLS/non TLS fran.b C Programming 13 03-25-2009 08:06 AM
/usr/bin/ld: errno: TLS definition in /lib/libc.so.6 nasim751 C Programming 4 02-25-2009 04:15 AM
Function argument assignment between types "unsigned int*" and "unsigned long*" nadeer78 C Programming 8 03-10-2008 11:57 AM
where from extern int errno came ? jabka C Programming 2 10-14-2007 05:25 PM
simulate Grep command in Unix using C laxmi C Programming 6 05-10-2002 04:10 PM


All times are GMT -6. The time now is 04:01 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22