Thread: problems using threads.....?

  1. #1
    stumpert
    Guest

    problems using threads.....?

    Hi I'm trying tom write a simple chat program. It is supposed to make one server, and one client. I run the program. One instanse is server the other is client. When I start the main thread listens to the socket, and prints everything out to the consol. My KeyboartThread gets everything from the keybord and puts it to the socket. My problem is that when I send somthing(types on the keybord) form the client, it doesn't appear on the client before I send somthing from the server. Understand? It looks like the thread is stuck at the cin command. Why doesn't the main thread write anything to the console befor the cin is finished? Have I locked the cosole by "writing" cin? How can i fix this? Here comes my code:

    #include <winsock.h>
    #include <iostream.h>
    #include <memory.h>
    #include <process.h>

    // Prototype functions
    SOCKET establish(unsigned short portnum);
    void Skelton_Server(void);
    void do_something(SOCKET s);
    SOCKET Skelton_Client(const char *hostname, unsigned short portnum);
    void CleintDoSomething(SOCKET s);
    int read_data(SOCKET s,char* buf,int n);
    int write_data(SOCKET s,char* buf,int n);
    const unsigned short PortNum=5000; // random port number

    void KeyboardThread(void *s);

    int main(int argc, char* argv[])
    {
    try
    {
    WSADATA info; // some sorta of struct
    // WSAStartup must be called fist when init winsock it takes the version number of winsock using in this
    // case 1.1 as a word ( MakeWord function does that ). The second param is the address of the WSADATA struct
    // it will return 0 if sucessful
    if(WSAStartup(MAKEWORD(1,1),&info)!=0)
    {
    throw("intilizing Winsock failed");
    }
    char choice;
    cout<<"Is this a server ?\t";
    cin>>choice;
    switch(choice)
    {
    case 'y':
    case 'Y':
    Skelton_Server();
    break;
    case 'n':
    case 'N':
    char name[32];
    SOCKET s;
    cout<<"Type in name of Server computer: \t";
    cin>>name;
    s=Skelton_Client(name,PortNum);
    if(s==INVALID_SOCKET){throw("could not connect");}
    CleintDoSomething(s);
    break;
    default:
    throw("Unknown command");
    break;
    }
    }
    catch(char* error)
    {
    cout<<error<<"\n";
    WSACleanup(); // Clean up Winsock
    return 0;
    }
    WSACleanup(); // Clean up Winsock
    return 0;
    }
    // This function establishes a socket binds it to address
    SOCKET establish(unsigned short portnum)
    {
    // Establish a socket
    SOCKET s; // Must ! be in try block cause ! seen outside of {}
    try
    {
    char name[256];
    sockaddr_in sa;
    hostent *hp;
    ZeroMemory(&sa,sizeof(sockaddr_in)); // clr mem
    gethostname(name,sizeof(name)); //our name ?
    hp=gethostbyname(name); // find address info
    if(hp==NULL){throw("Invalid Socket");}
    sa.sin_family=hp->h_addrtype; // this is the host address
    sa.sin_port=htons(portnum);
    // Create Socket ,AF_INET flag makes work on internet only
    s=socket(AF_INET,SOCK_STREAM,0);
    if(s==INVALID_SOCKET){throw("Invalid Socket");}
    // bind socket to the internet address , ie it gives
    // address to what it is listening to ( ie server)
    if(bind(s,(sockaddr*)&sa,sizeof(sockaddr_in))==SOC KET_ERROR)
    {
    throw(1);
    }
    }
    catch(char* error)
    {
    cout<<error<<"\n";
    return INVALID_SOCKET;
    }
    catch(...)
    {
    cout<<"Socket Error"<<"\n";
    closesocket(s);
    return INVALID_SOCKET;
    }
    listen(s,3); // allow max of 3 packets in que
    return (s);
    }
    // Skelton server that waits 4 incoming calls using accept function
    // So this creates socket that waits for incoming calls
    void Skelton_Server(void)
    {
    SOCKET s;
    if((s=establish(PortNum))==INVALID_SOCKET){exit(1) ;return;}
    // Wait 4 phone calls
    for(;
    {
    SOCKET new_sock= accept(s,NULL,NULL);
    if(s==INVALID_SOCKET){exit(1);}
    do_something(new_sock);
    closesocket(new_sock);
    break;
    }
    return;
    }

    // This is a client function that creates a socket (phone line)
    // that instead of waiting for calls , connects to a server socket
    // & phones it up ( connects to it). Allowing data to be transfered
    // through sockets
    SOCKET Skelton_Client(const char *hostname, unsigned short portnum)
    {
    sockaddr_in sa;
    hostent *hp;
    SOCKET s;
    hp=gethostbyname(hostname);
    if(hp==NULL){return INVALID_SOCKET;}
    // Clrs the memory in struct
    ZeroMemory(&sa,sizeof(sockaddr_in));
    // Set mem address
    memcpy((char*)&sa.sin_addr,hp->h_addr,hp->h_length);
    sa.sin_family=hp->h_addrtype;
    sa.sin_port=htons((u_short)portnum);
    s=socket(hp->h_addrtype,SOCK_STREAM,0);
    if(s==INVALID_SOCKET){return INVALID_SOCKET;}
    if(connect(s,(struct sockaddr*)&sa,sizeof(sa))==SOCKET_ERROR)
    {
    closesocket(s);
    return INVALID_SOCKET;
    }
    return s;
    }

    // Does stuff with new socket
    void do_something(SOCKET s)
    {
    _beginthread( KeyboardThread, 0, (void *) (s) );
    while(true)
    {
    char buffer[256];
    for(int i=0;i<256;i++){buffer[i]='\0';}
    int j=read_data(s,buffer,sizeof(buffer));
    if(j!=-1){cout<<buffer<<"\n";} // output data passed
    }
    return;
    }

    // Does stuff for client
    void CleintDoSomething(SOCKET s)
    {
    _beginthread( KeyboardThread, 0, (void *) (s) );
    // Waits 4 data writes something sends it then waits
    while(true)
    {
    char buffer[256];
    for(int i=0;i<256;i++){buffer[i]='\0';}
    int j=read_data(s,buffer,sizeof(buffer));
    if(j!=-1){cout<<buffer<<"\n";} // output data passed
    }
    return;
    }

    int read_data(SOCKET s,char* buf,int n)
    {
    int bcount=0;
    int br=0;
    while(bcount<n)
    {
    if((br=recv(s,buf,n-bcount,0))>0)
    {
    bcount+=br;
    buf+=br;
    }
    else if(br<0)
    {
    return -1;
    }
    }
    return bcount;
    }

    int write_data(SOCKET s,char* buf,int n)
    {
    int bcount=0;
    int br=0;
    while(bcount<n)
    {
    if((br=send(s,buf,n-bcount,0))>0)
    {
    bcount+=br;
    buf+=br;
    }
    else if(br<0)
    {
    return -1;
    }
    }
    return bcount;
    }
    // Info socket is like some sorta of communication. So when u make
    // a socket its like connecting a phone line. There a diffrent types
    // of sockets ( the one we use is Internet AF_INET).

    void KeyboardThread(void *s)
    {
    SOCKET sock = (SOCKET) s;
    while(true)
    {
    char string[256];
    for(int i=0;i<256;i++){string[i]='\0';}
    cin>>string;
    write_data(sock,string,sizeof(string));
    }
    /* _endthread given to terminate */
    _endthread();
    return;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I would love to help, but zero indention code activates my most feared defensive measures: sarcasm and stingy mode.

    -Prelude
    My best code is written with the delete key.

  3. #3
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    What he/she means is, edit your post and use code tags.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Yet another n00b in pthreads ...
    By dimis in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2008, 12:43 AM
  2. How to find O of threads ?
    By jabka in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 12:25 PM
  3. How to get tid of kernel threads?
    By micke_b in forum C Programming
    Replies: 4
    Last Post: 10-19-2007, 07:25 AM
  4. Replies: 0
    Last Post: 10-06-2007, 01:25 PM
  5. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM