dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 25 of 25

Thread: socket programming error

  1. #16
    Join Date
    Sep 2011
    Posts
    11

    Re: socket programming error

    i think it's better to put the codes here and u see that !
    how can i do that ? just copy and paste here ?

  2. #17
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,331

    Re: socket programming error

    I would think it would be documented if it accepts UDP transimissions.
    Code tags can be typed as

    [code]your code would go here[/code]

    which would appear as
    Code:
     your code here
    The benift of the code tags is it allows the indenting to remain in the code and makes it much easier to read.
    Always use [code][/code] tags when posting code.

  3. #18
    Join Date
    Sep 2011
    Posts
    11

    Re: socket programming error

    client :
    Code:
     
    static void Main(string[] args)
            {
                byte[] data = new byte[1024];
                string input, stringData;
                IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
                
                Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                string welcome = "Hello !!";
                data = Encoding.ASCII.GetBytes(welcome);
               
                server.SendTo(data, data.Length, SocketFlags.None, ipep);
                IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
                EndPoint Remote = (EndPoint)sender;
                data = new byte[1024];
                int recv = server.ReceiveFrom(data, ref Remote);
    
                Console.WriteLine("Message received from {0}:", Remote.ToString());
                Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
    
                while (true)
                {
                    input = Console.ReadLine();
                    if (input == "exit")
                        break;
                    server.SendTo(Encoding.ASCII.GetBytes(input), Remote);
                    data = new byte[1024];
    
                    recv = server.ReceiveFrom(data, ref Remote);
                    stringData = Encoding.ASCII.GetString(data, 0, recv);
                    Console.WriteLine("stop ! ");
                    server.Close();
                }
            }
    server
    Code:
    static void Main(string[] args)
            {
                int recv;
                byte[] data = new byte[1024];
                IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);
                Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                
                newsock.Bind(ipep);
                Console.WriteLine("waiting for a client...");
                IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
                EndPoint Remote = (EndPoint)(sender);
                recv = newsock.ReceiveFrom(data, ref Remote);
                
                Console.WriteLine("Message recevied from {0}:", Remote.ToString());
                
                Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
                string welcome = "Welcome !";
                data = Encoding.ASCII.GetBytes(welcome);
                newsock.SendTo(data, data.Length, SocketFlags.None, Remote);
    
                while (true)
                {
                    data = new byte[1024];
                    recv = newsock.ReceiveFrom(data, ref Remote);
                    Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
                    newsock.SendTo(data, recv, SocketFlags.None, Remote);
                }
            }

  4. #19
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,331

    Re: socket programming error

    Now I'm confused. You said that you did not write the server program but here you are showing code for it? So is this a different program or ? Is it still the same error? Are you getting anything at the server?
    Always use [code][/code] tags when posting code.

  5. #20
    Join Date
    Sep 2011
    Posts
    11

    Re: socket programming error

    why u confused !! I mean that i don't write it by myself ! this is a program and i want to test it but that error occured.
    no , nothing i get !

  6. #21
    Join Date
    Sep 2011
    Posts
    11

    Re: socket programming error

    can u test it plz ?

  7. #22
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,331

    Re: socket programming error

    The reason for asking the question was to see if you had the code or if you were attempting to talk to a program written by a 3rd party. Have you placed a break point and stepped through the code on the client or server? If not then this should be your first step when a program does not work as expected.
    Last edited by DataMiser; September 11th, 2011 at 07:54 AM.
    Always use [code][/code] tags when posting code.

  8. #23
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,331

    Re: socket programming error

    I loaded your code and tried it The error you mention happens if you try to run the client with the server not already running. When the server is running the first attempt from the client did nothing it just hung waiting for input when none was there. I restarted the client and saw Hello show up on the server and then the client hung waiting for input.

    Not sure what the goal is here but when it comes to reading data from a port be it TCP, UDP or whatever you need to make sure data is there before you try to read it otherwise you are going to hang on that line until data arrives and if it never arrives you are locked up.
    Always use [code][/code] tags when posting code.

  9. #24
    Join Date
    Sep 2011
    Posts
    11

    Re: socket programming error

    ok ... so what exactly should I do now !?
    u mean that I run the client very soon , how u restarted client ... !

    why this happen , u say hanging on lines !

  10. #25
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,331

    Re: socket programming error

    To get it to work I built the server and ran the server EXE first.
    Then I went into the IDE and started the client in debug mode so i could see what it was doing.
    The first time the client hung on a receive line [you can hit the pause button in the IDE and if a line of code is executing it will highlight the line]
    I stopped the client then started it again [server is still running the whole time]
    I see hello appear on the server window
    Client hangs again this time on the next receive line.

    You need to run your program in debug mode and step through the code to see what is happening. UDP is not a good way to send anything as there are no assurances that what you send will get to where you sent it. It is easier to use than TCP but TCP is far better. In either case you do not issue a read statement without first checking to see if there is data there to be read and you should have a way that the program can break out of the waiting for data state if no data appears in x amount of time.
    Always use [code][/code] tags when posting code.

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)