Need an example of client socket sending data

Need an example of client socket sending data to server
written for windows please with out MFC

it can be in c or c++

Any information will be appreciated

Regards
neta
netaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ufolk123Commented:
Hi neeta,

#include <stdio.h>
#include <winsock.h>
#include <string.h>
#include <stdlib.h>
#include<windows.h>

#define SERVERPORT 5555
#define CLIENTPORT 5554

int SockId=0;
static int iCount=0;


int InitialiseClient(void)
{
    int error;
      SOCKADDR_IN SockData;
      SOCKADDR_IN SockSock;
    int sockid;
    WORD wVersionRequested;
      WSADATA wsaData;
    wVersionRequested = MAKEWORD( 2, 0 );
    error = WSAStartup( wVersionRequested, &wsaData );
        if ( error != 0 ) {exit(0);}
      
      SockData.sin_family = AF_INET;
      SockData.sin_port = htons( CLIENTPORT );
    SockData.sin_addr.s_addr=htonl(INADDR_ANY);

      sockid = socket ( AF_INET , SOCK_DGRAM ,0);
      
      if ( sockid == -1 )
      {
          printf ( "\n Socket Failed %d\n",WSAGetLastError() );
            exit ( 1 );
      }
      error = bind ( sockid , (struct sockaddr *)&SockData , (int ) sizeof ( SockData ) );
      if ( error )
      {
          printf ( "\n  Bind Failed %d \n" ,WSAGetLastError());
            closesocket( sockid );
          exit(1);
      }

      SockSock.sin_family = AF_INET;
      /*Connect to server at IP:192.168.174.32 on port SERVER_PORT*/
      SockSock.sin_port = htons(SERVER_PORT );
    SockSock.sin_addr.s_addr=inet_addr("192.168.174.32");
      
      int iState=connect ( sockid,(sockaddr FAR*)&SockSock,sizeof(SockSock));    
      printf("State of connection =%d ",iState);fflush(stdout);
      return sockid;
}



void main(void )
{
    char command[256];
    char header[2],result[256];
      int iCount=1;
      SockId=InitialiseClient();

    header[0]=1;
      command[0]=iCount;
      header[1]=(char)1;
      header[2]=0;

    strcpy(&command[1],"KILL PROCESS");                  
      
      if(send(SockId,header,1,0)!=1)
      {
      printf("Serious Problem header");
      }
      printf(" Problem header=%s data=%d",header,strlen(header));


      int Data=recv(SockId,result,1,0);
      
      if(Data!=1)
            {
          printf("Recv :Len=%d LineLen=%d Error=%d",Data,2,WSAGetLastError());
          exit(1);
            }




This code connects to a UDP server listening on port SERVER_PORT.
You may need to code the server portion also.


Regards,
ufolk123
0
netaAuthor Commented:
Thanks you all for your replay
My problem is how to get the server ip
by his name.

i need to establish the connection
with the server with out knowing his
ip.
only with knowing his HOSTNAME
How should i change the example ufolk123
send ?

Best regards
Neta !!!

0
netaAuthor Commented:
Thanks you all for your replay
My problem is how to get the server ip
by his name.

i need to establish the connection
with the server with out knowing his
ip.
only with knowing his HOSTNAME
How should i change the example ufolk123
send ?

Best regards
Neta !!!

0
chensuCommented:
Use the gethostbyname function.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.