socket problem

This is a discussion on socket problem within the C++ Programming forums, part of the General Programming Boards category; << SPLIT FROM http://cboard.cprogramming.com/cplus...rning-odd.html >> 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 ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 03: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, 09:01 AM

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