Communication ~Please no flaming

This is a discussion on Communication ~Please no flaming within the C++ Programming forums, part of the General Programming Boards category; I'm trying to make a client-server program that writes to C:\message.txt to the server, I *think* I have everything down ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    241

    Communication ~Please no flaming

    I'm trying to make a client-server program that writes to C:\message.txt to the server, I *think* I have everything down before the acual communication, thats when I'm stumped :-/ can someone help me out pelase?
    Code:
    #include <iostream>
    #include <windows.h>
    #include <fstream>
    #include <string>
    
    int main()
    {
        char message[NULL];
        int first[3];
        int second[3];
        int third[3];
        void ip[NULL];
        std::cout<<"Hello, welcome to my first (client-server) program";
        std::cout<<"/n"<<"Please enter your clients ip address:";
        //This is for the ip address
        std::cin>>first;
        std::cout<<".";
        std::cin>>second;
        std::cout<<".";
        std::cin>>third;
        std::cout>>"/n";
        std::strcmp (first,.,second,.,third)=ip;
        //end ip address
        //this is for message
        std::cout>>"Thank you please enter message:";
        std::cin<<message;
        std::ofstream a_file (C:\message.txt);
        std::a_file<<message;
        std::a_file.close();
        //end message
        //starting communication

  2. #2
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Using Windows? If so: Winsock....
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Yes, I am using windows, yet I know little about Winsock, I just want to know if I am starting out the program right (getting everything right before the communication).

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    What problem are you having? Your brackets are backward for cin and cout (<< vs. >>), you need to put the file path into double quotes and add an escape character for the \. You #include <string> but don't use string. Instead you use C style strings and strcmp, which is in <cstring> and compares strings. You should make first, second and third string variables, and use + to combine them.

    There are many more errors. Your best bet is to start off with an empty project and add a line of code at a time, then make sure it compiles before adding the next line. When you get compile errors try to fix them and come here if you are stuck. Once the code compiles you can figure out if it is where you want to go.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    I have just added those incase I feel like adding some features.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    I do appreciate the quick responses

  7. #7
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    For my code below to work you need to link the winsock library. Take a look at this sample client code I wrote up real quick:
    Code:
    #include <windows.h>
    #include <winsock.h>
    
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
    {
        WSADATA wsaData;
        WORD sockVer;
        int nret;
        
        sockVer=MAKEWORD(1,1);
        
        WSAStartup(sockVer,&wsaData);
        
        SOCKET sock;
        
        sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
        if(sock==INVALID_SOCKET)
        {
             WSACleanup();
             return 0;
        }
        LPHOSTENT server;
        server=gethostbyname("SERVERPC"); //Change this to the name of the computer running the server
        if(!server)
        {
             WSACleanup();
             return 0;
        }
        SOCKADDR_IN serverInfo;
        
        serverInfo.sin_family=AF_INET;
        serverInfo.sin_addr=*((LPIN_ADDR)*server->h_addr_list);
        serverInfo.sin_port=htons(777);  //Going to be using port 777
        
        nret=connect(sock,(LPSOCKADDR)&serverInfo,sizeof(struct sockaddr));
        if(nret==SOCKET_ERROR)
        {
             WSACleanup();
             return 0;
        }
        char msgbuffer[256];
        ZeroMemory(msgbuffer,256);
        strcpy(msgbuffer,"Our message to post");  //Our message to send
        nret=send(sock,msgbuffer,strlen(msgbuffer),0);
        if(nret==SOCKET_ERROR)
        {
             WSACleanup();
             return 0;
        }
        closesocket(sock);
        MessageBox(NULL,"Your message successfully sent to server!","Success",0);
        WSACleanup();
        return 0;
    }
    As you can see it has a set message, I'm assuming you can change it so the user can enter that message. It also has a set server computer name which in the example is SERVERPC. There is a way you can use an IP address, which can be found on this very useful tutorial. On that page there is also a tutorial on making a server, sending and receiving, etc. When I first started Winsock I used that tutorial.
    Last edited by jmd15; 11-18-2005 at 05:03 PM.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Thank you, I appreciate it.

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    My linker isnt wanting to link the WSACleanup(); line, its giving an "Undefined refrence to @WSACleanup" I believe.

  10. #10
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Gotta link libwsock32.a. Oh and I was feeling generous and wrote you a working server code:
    Code:
    #include <windows.h>
    #include <winsock.h>
    #include <fstream.h>
    
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
    {
        WORD sockVer;
        WSADATA wsaData;
        int nret;
        
        sockVer=MAKEWORD(1,1);
        
        WSAStartup(sockVer,&wsaData);
        
        SOCKET sock;
        
        sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
        if(sock==INVALID_SOCKET)
        {
             WSACleanup();
             return 0;
        }
        SOCKADDR_IN serverInfo;
        serverInfo.sin_family=AF_INET;
        serverInfo.sin_addr.s_addr=INADDR_ANY;
        serverInfo.sin_port=htons(777);
        
        nret=bind(sock,(LPSOCKADDR)&serverInfo,sizeof(struct sockaddr));
        if(nret==SOCKET_ERROR)
        {
             WSACleanup();
             return 0;
        }
        nret=listen(sock,1);
        if(nret==SOCKET_ERROR)
        {
             WSACleanup();
             return 0;
        }
        MessageBox(NULL,"Server waiting for connection","Waiting",0);
        SOCKET client;
        client=accept(sock,NULL,NULL);
        if(client==INVALID_SOCKET)
        {
             WSACleanup();
             return 0;
        }
        char received[256];
        ZeroMemory(received,256);
        nret=recv(client,received,256,0);
        if(nret==SOCKET_ERROR)
        {
             WSACleanup();
             return 0;
        }
        closesocket(client);
        closesocket(sock);
        WSACleanup();
        ofstream afile("C:\\messages.txt");
        afile<<received;
        afile.close();
        MessageBox(NULL,"Message successfully received and place in messages.txt","Success",0);
        return 0;
    }
    You don't have to modify anything from this server code, just modify the client code to let the user enter the message and your good to go.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  11. #11
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    I know this may sound very stupid, but how do I link to libwsock32.a?

  12. #12
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Dev-C++? If so:
    Attached Images Attached Images  
    Last edited by jmd15; 11-18-2005 at 05:39 PM.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  13. #13
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Also, may you explain to me what are the functions you are calling and almost everything else, example code works good, when troubleshooting, but when I am trying to write code, and using your example for refrence, I cannot decipher (I dont know if I spelt that right) what the functions are doing; example being WINAPI. ( I do appreciate the effort though of you writing the code)

    Edit:Thank you for showing me how to link

  14. #14
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    No problem, now to explaining it all.
    Code:
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
    {
    That is just like the entry point for a Win32 application, think of it as the Windows version of int main().
    Code:
        WORD sockVer;  //Initializing the word variable to hold which version of Winsock we want to use(which is 1.1)
        WSADATA wsaData;  //The structure that holds our data needed for startup,etc
        int nret;  //Just an integer that I use to check if the functions failed or not
        
        sockVer=MAKEWORD(1,1);  //Giving sockVer the right value so when passed to WSAStartup it knows to use version 1.1
        
        WSAStartup(sockVer,&wsaData);  /*Starting up WSA which stands for
                                         Windows Socket Architecture(NEEDS TO BE INITIALIZED TO USE WINSOCK!!!)*/
    Now for this bit:
    Code:
    SOCKET sock;  //Creating a variable of type SOCKET to use
        
        sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);  //Pretty self-explanatory
    The other functions like bind, accept, recv, send, and listen are pretty much self-explanatory. They do just what their name says. If you want to see what the parameters are, go to MSDN and look them up.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  15. #15
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Oh, personally I think that you should get into the Win32 API BEFORE getting into Winsock. That way you have an understanding of the Win32 API, how it works, and the few Win32 API functions you need when using Winsock. Also, when you write programs that use Winsock, you could make it look pretty by adding a GUI if you got into the Win32 API first.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unkown Hang
    By Bladactania in forum C Programming
    Replies: 31
    Last Post: 04-22-2009, 09:33 AM
  2. Communication between programs?
    By johny145 in forum Windows Programming
    Replies: 3
    Last Post: 06-01-2005, 10:14 PM
  3. Looking for communication lib
    By BrownB in forum C Programming
    Replies: 3
    Last Post: 04-27-2005, 10:01 AM
  4. Serial communication packets
    By Roaring_Tiger in forum C Programming
    Replies: 3
    Last Post: 04-26-2003, 08:33 AM
  5. communication
    By in need of help in forum C Programming
    Replies: 3
    Last Post: 12-27-2002, 02:56 PM

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