CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Guest

    need a programm of client & server with windows sockets!!!!!!!

    Hi all,
    i want the programms of client &server with windows sockets not BSD sockets.

    Thanks


  2. #2
    Join Date
    May 1999
    Posts
    6

    Re: need a programm of client & server with windows sockets!!!!!!!

    Derive a class from CSocket(MySocket).
    take a object of ur MySocket.

    server:
    ServerSocket.Create(localport);
    ServerSocket.listen();

    client:
    MySocket.Connect(remothostadd,remotport);
    Mysocket.SendData(mydata);

    server:
    ServerSocket.recive(buffer,sizeof(buffer));



  3. #3
    Guest

    Re: need a programm of client & server with windows sockets!!!!!!!Urgent

    i did one programm but it dos not work, there is pb, i dont know how to make it work:

    this is a server pogramm:

    #include <winsock.h>
    #include <stdio.h>
    //#include <winbase.h>
    #include <string.h>
    #include <stdlib.h>
    #include <stddef.h>




    u_short port = 2000;


    main()
    {


    WSADATA wsadata;
    WORD wVer;
    SOCKET sd_listen,sd_accept;
    int nRc,nlen,nbc, nlc;

    FILE *fp;
    char buffer[1000];


    struct sockaddr_in addr_srv;


    //if (argc>2) {
    // fprintf(stderr, "usage : %s port \n", argv[0]);
    // exit(3);
    //}


    wVer= MAKEWORD(1,1);
    nRc = WSAStartup(wVer,&wsadata);
    if (nRc != 0)
    {
    fprintf(stderr,"error use socket \n");
    exit(1);
    }


    sd_listen=socket(PF_INET,SOCK_STREAM,0);
    if (sd_listen == INVALID_SOCKET)
    {
    //do error processing and return
    fprintf(stderr,"error opening socket \n");
    exit(1);
    }
    addr_srv.sin_family = PF_INET;
    addr_srv.sin_port = htons(2000);
    addr_srv.sin_addr.s_addr = htonl(INADDR_ANY);


    nbc = bind(sd_listen,(struct sockaddr FAR *)&addr_srv,sizeof(addr_srv));

    if (nbc == SOCKET_ERROR)
    {
    // do error processing and return
    fprintf(stderr, "error connecting socket \n");
    exit(1);
    }

    nlc = listen(sd_listen, 4);
    if (nlc == SOCKET_ERROR)
    {
    // do error processing and return
    fprintf(stderr, "error listening socket \n");
    exit(1);
    }


    //need a way to determine when to do accept()
    sd_accept = accept(sd_listen,NULL,NULL);

    if (sd_accept == INVALID_SOCKET)
    {
    // do error processing and return
    fprintf(stderr, "error accepting socket \n");
    exit(1);
    }

    fprintf(stderr, "connection established \n");


    // when connect "completes" can send() and recv() data
    // socket sd_accept. Socket sd_listen is still listening

    while ((nlen = recv(sd_accept, (char FAR *) &buffer, 1000,0))>0)
    {
    fwrite (buffer, 1, nlen, fp);
    }


    if (nlen == SOCKET_ERROR)
    {
    fprintf(stderr, "Receive error \n");
    exit(1);
    }


    }

    this is client programm :

    //ADC+CLIENT source code

    #include <windows.h>
    #include <stddef.h>
    #include <stdio.h>
    #include <stdlib.h>
    //#include <winioctl.h>
    #include <string.h>
    //#include <winbase.h>
    #include <winsock.h>

    u_short toport = 2000;

    int main(int argc, char *argv[])
    {


    WSADATA wsadata;
    WORD wVer;


    SOCKET sd_cl;
    int nRc, nlen;

    FILE *fp;

    struct sockaddr_in addr_srv;

    char buffer[1000];

    if (argc !=2) {
    fprintf(stderr, "Usage: %s host\n", argv[0]);
    exit(3);
    }

    fprintf(stderr,"%d\n", toport);


    wVer= MAKEWORD(1,1);
    nRc = WSAStartup(wVer,&wsadata); //Load the Winsock DLL in memory
    if (nRc != 0)
    {
    fprintf(stderr,"error use socket \n");
    exit(1);
    }




    // create client socket
    sd_cl = socket(PF_INET, SOCK_STREAM, 0);
    if(sd_cl == INVALID_SOCKET)
    {
    // error occurred on socket call
    fprintf(stderr,"error opening socket \n");
    exit(1);
    }

    addr_srv.sin_family=PF_INET;

    addr_srv.sin_port=htons(toport);

    addr_srv.sin_addr.s_addr = inet_addr("130.132.26.154");





    // request connection
    nRc = connect(sd_cl,(struct sockaddr FAR *)&addr_srv,sizeof(addr_srv));

    if (nRc == SOCKET_ERROR)
    {
    // do error processing and return
    fprintf(stderr,"error connecting socket \n");
    exit(1);
    }

    // when connect "completes" can send() and recv() data
    // build and send request

    fp = fopen("test.dat", "rb");
    if (fp == NULL) {
    fprintf(stderr, "can't open the file\n");
    exit(1);
    }


    while (fgets(buffer, 1000, fp) != NULL) {
    //n = strlen(buffer);
    //nleft = n;

    //while (nleft >0) {
    nlen = send(sd_cl,(char FAR *) &buffer, sizeof(buffer),0);

    if (nRc== SOCKET_ERROR)
    {
    fprintf(stderr,"error sending message \n");
    exit(1);
    }
    //nleft -=len;
    // +=len;
    // }
    //return(n-nleft);

    }
    fclose(fp); //Close file

    }






Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured