Thread: socket problem

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    1

    Question socket problem

    << SPLIT FROM socket connect returning odd >>
    Code:
    #include<winsock.h>
    #include<stdio.h>
    #include<iostream.h>
    #include<windows.h>
    #include<conio.h>
    #include<string.h>
    
    
    /*Global data for sending and receiving using communication layer*/
    
    	WSADATA wsa_data;
    	SOCKET Socket;
       SOCKET tempSocket;
    	SOCKADDR_IN address;
    	char data[50],ch;
    
    
    void comm_send(char ch[],int size);
    void comm_receive();
    void comm_init();
    void getInput();
    void comm_close();
    
    
    
    main(){
    
    
    int index=0;
    
      comm_init();
      cout<<"Receiving"<<endl;
      comm_receive();
      if(!strcmp(data,"red"))
      cout<<"Stop"<<endl;
      else if(!strcmp(data,"green"))
           cout<<"Go"<<endl;
           else if(!strcmp(data,"yellow"))
                cout<<"Ready"<<endl;
                else if(!strcmp(data,"blue"))
                     cout<<"Blocked"<<endl;
                     else if(!strcmp(data,"orange"))
                          cout<<"One way"<<endl;
      comm_close();
      cout<<"Bye bye";
    
    getch();
    
    
    }
    
    
    /******************************************************/
    
    void getInput(){
    
    int index;
      		do{
         		ch=getche();
            	data[index]=ch;
             index++;
      	   }while(ch!='\r');
          data[index-1]='\0';
    
    
    }
    
    
    /*-----------------------------------------------------*/
    
    void comm_init(){
    
    	int result;
    	result=WSAStartup(1.1,&wsa_data);
     	Socket=socket(AF_INET,SOCK_STREAM,0);
    
       address.sin_port=50;
       address.sin_family=AF_INET;
    
       address.sin_addr.S_un.S_un_b.s_b1=127;
    	address.sin_addr.S_un.S_un_b.s_b2=0;
    	address.sin_addr.S_un.S_un_b.s_b3=0;
    	address.sin_addr.S_un.S_un_b.s_b4=1;
    
    	result=bind(Socket,(SOCKADDR *)&address,sizeof(address));
    
    	connect(Socket,(SOCKADDR *)&address,sizeof(address));
    
    }
    
    void comm_send(char data[], int size){
       int result;
       result=send(Socket,data,50,0);
    }
    
    void comm_close(){
    closesocket(Socket);
    Socket=NULL;
    tempSocket=NULL;
    }
    
    
    
    void comm_receive(){
       int result;
    	listen(Socket,1);
       tempSocket=accept(Socket,NULL,NULL);
       cout<<"Connection established successfully"<<endl;
       Socket=tempSocket;
       //result=recv(Socket,data,50,0);
       do{
    
          result=recv(Socket,data,50,0);
          cout<<"Data Received = "<<data<<endl;
       }while(result!=SOCKET_ERROR);
    
    }






    this program srablishes connection itself with out server and ends.............what is the problemmmmm
    Last edited by Salem; 03-20-2010 at 06:25 AM. Reason: Splitting from the bone yard, and code tags

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > what is the problemmmmm
    You hijacked a thread dead for about 6 years and you didn't use code tags.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    too many global variables?
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-blocking socket connection problem
    By cbalu in forum Linux Programming
    Replies: 25
    Last Post: 06-03-2009, 02:15 AM
  2. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  3. socket message sending and receiving problem
    By black in forum C Programming
    Replies: 5
    Last Post: 01-15-2007, 04:46 AM
  4. sockets problem programming
    By kavejska in forum C Programming
    Replies: 0
    Last Post: 07-25-2005, 07:01 AM
  5. Client/Server Socket Receive Problem
    By mariabair in forum Networking/Device Communication
    Replies: 6
    Last Post: 12-25-2003, 10:01 AM