VC++ socket

This is a discussion on VC++ socket within the Windows Programming forums, part of the Platform Specific Boards category; I have been doing socket programming lately. I then tried to implement it on Visual C++. I encountered a few ...

  1. #1
    1337
    Join Date
    Jul 2008
    Posts
    135

    VC++ socket

    I have been doing socket programming lately. I then tried to implement it on Visual C++. I encountered a few problems. I will post the codes first.

    Form1.h
    Code:
    #include <string>
    public:
    char* str;
    	 
    System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    	     String^ temp = textBox1->Text;
    	     MarshalString(str, temp); //convert String^(temp) to char*(str)
    	      if ((result2 = send(sockfd, str, strlen(str), 0)) == -1)  //flags are left to 0
    	            {
    	                printf("Send failed: %d\n", result2);
    	                exit(1);
    	            }

    Server.cpp
    Code:
    #include "stdafx.h"
    #include "Form1.h"
    #include <windows.h>
    #include <winsock2.h>
    
    
    if ((result2 = send(sockfd, msg, strlen(msg), 0)) == -1)  //flags are left to 0
    {
    printf("Send failed: %d\n", result2);
    return 1;
    }
    Note that i have excluded irrelevant code. Here are my Form1.h and Server.cpp. I have a textbox, "textBox1" which i will trype something in it before i click "button1" to send something to the client. However, i do not know how to listen on the event button1_Click in Server.cpp

    Here is my pseudocode of what i wish to do.
    Code:
    listen for button1 click event
    if button is click
    get the message from the textbox1 and store it into the str variable
    initiate send

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So... have you done accept? connect? Any of the other networking bits at all?

  3. #3
    1337
    Join Date
    Jul 2008
    Posts
    135
    Yes, i have done all. It is working perfectly. But now, what i am trying to do is, whenever i click the button1, i want to get the text from textBox1 and send it to the client using send().

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Oh, so you want to make a form. How have you tied these together? Is Server the parent of form1? Are they the same application? Can you send a message to Server from form1?

    (EDIT: For instance searching MSDN gave http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx which may be a bit overkill but looks roughly like what you're trying to do.)
    Last edited by tabstop; 07-30-2011 at 11:42 PM.

  5. #5
    1337
    Join Date
    Jul 2008
    Posts
    135
    Thank you for answering my questions. Let me clarify further.
    1. Server.ccp is where the main() resides.
    2. Form1.h is where the event listener resides (by default - after i double click the button1 for the first time).
    3. both files are in the same project.
    4. In the project, From1.h resides in the "Header Files" folder, while the Server.cpp resides in the "Source Files" folder
    5. I didnt try sending message from Form1. What i did was, in Server.cpp, where the main() is, i declared and string and used send(). Everything is done in the main().

    I hope it is clearer now. I am sorry for not being clear.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    You currently have a 'Server' object and a 'Form1' object and need both to communicate (ie the Form1 object to pass the send command to the Server object.)

    You could decide that the Form1 object should be the parent and the Server object one of its children or via versa or have another object as parent and both Form1 and Server as its children.

    Lets go with the Server object as a child of the Form1 (ie make Form1 the boss and let the Form1 control the Server object).

    In the Server Object create a method that will receive a string as a param and send the string to the client, returning the result (lets call it SendMsg() ).

    Add a Server object as a member of the Form1 (Server m_TheServer).
    Add an OnCreate (or OnInit) message handler to Form1 (WM_CREATE or WM_INITDLG)
    In this handler set up and connect the Server object.

    Add a handler for the button click.
    In the button click handler use get the text from the edit (GetDlgItemText() or GetWindowText() or use use the edits member variable)
    Call the member Server objects SendMsg() (ie m_TheServer.SendMsg( StringToSend ) )

    If you want the Server object as the parent then send in a pointer to the Server object as the lParam of your CreateWindow call, recieve it in the Form1 as the lpCreateParams of the CREATESTRUCT and set it to a member variable. Use in a similar way as alredy described.

    Give this a go and post again if you need teh code corrected.
    Last edited by novacain; 07-31-2011 at 02:24 AM. Reason: clarity
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    1337
    Join Date
    Jul 2008
    Posts
    135
    I made Server.cpp as the parent. So, here is the code

    server.cpp
    Code:
            Form1 newForm1;
    	char* str;
    	newForm1.button1_Click(newForm1, newForm1.button1_Click)
    	{
    		String^ temp = newForm1.textBox1->Text;
    		newForm1.MarshalString(str, temp);
    		send(sockfd, str, strlen(str), 0);
    	}

    Form1.h
    Code:
    public: System::Windows::Forms::Button^  button1;
    public: System::Windows::Forms::TextBox^  textBox1;
    public: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e){}
    Am i doing the right thing?
    Another question is, what arguments should i pass for "newForm1.button1_Click()" in Server.cpp ? (to call the funtion from Form1.h)Thank you.

  8. #8
    1337
    Join Date
    Jul 2008
    Posts
    135
    I have attached the Server.cpp and form1.h files. Please guide me how i can accomplish this. I would prefer to make the Server.cpp as the parent as the main() resides there.
    Attached Files Attached Files

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    Quote Originally Posted by valthyx View Post
    I have attached the Server.cpp and form1.h files. Please guide me how i can accomplish this. I would prefer to make the Server.cpp as the parent as the main() resides there.
    I do not think the way you have set this up will work.

    Your main() should not do anything but start the Form1.

    This is because when you call Application::Run() the Form1 starts and main() 'stops'.

    That is while Form1 is running the code below Application::Run() is not called.

    Only once Form1 stops does your socket code actually get run.

    (ie until Form1 is finished the code is 'stuck' on the Application::Run() line)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  10. #10
    1337
    Join Date
    Jul 2008
    Posts
    135
    Ok, Here are my new setups. I could not get the socket initialisation dont. The error messagebox "Socket Failed" was called. I have attached new ones.
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Raw socket
    By like_no_other in forum Networking/Device Communication
    Replies: 4
    Last Post: 03-28-2009, 02:05 PM
  2. Socket programming in C with socket.h
    By funzy in forum Networking/Device Communication
    Replies: 13
    Last Post: 08-29-2008, 04:12 AM
  3. Socket's
    By elitesyntax in forum Networking/Device Communication
    Replies: 4
    Last Post: 03-29-2004, 12:00 PM
  4. n00b doing a Socket operation on non-socket
    By Kinasz in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-25-2004, 02:29 AM
  5. TCP/IP Socket
    By Nor in forum Linux Programming
    Replies: 4
    Last Post: 04-22-2002, 11:21 AM

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