4 Replies - 4925 Views - Last Post: 26 June 2011 - 01:12 AM Rate Topic: -----

#1 morsev  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 70
  • Joined: 26-September 10

simple question in socket programming

Posted 25 June 2011 - 02:03 PM

Hi guys.
iam beginner in socket programming
i made a simple server and client programs to send an receive a string from (the string sent from server to client)

The server code :
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint iep = new IPEndPoint(IPAddress.Any, 100);
            socket.Bind(iep);
            socket.Listen(4);
            socket.Accept();
            Console.WriteLine("we get the connection");
            string s = "samer";
            byte[] buffer = Encoding.ASCII.GetBytes(s);
            socket.Send(buffer);



The client code :
private void button1_Click(object sender, RoutedEventArgs e)
        {
            Socket client_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //IPEndPoint iep = new IPEndPoint(IPAddress.Any, 100);
            //client_socket.Bind(iep);
            client_socket.Connect(textBox1.Text, 100);
            byte[] buffer=new byte[1024];
            client_socket.Receive(buffer);
            textBox2.Text = Encoding.ASCII.GetString(buffer);
        }



The problem appends in server program in (socket.send(buffer)) the compiler said that :
An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in System.dll

Additional information: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied

so what the problem exactly ??

Is This A Good Question/Topic? 0
  • +

Replies To: simple question in socket programming

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5571
  • View blog
  • Posts: 11,904
  • Joined: 02-June 10

Re: simple question in socket programming

Posted 25 June 2011 - 02:17 PM

Quote

So what exactly is the problem?

Your code causes/raised an exception, and you didn't handle it.
Thus "An unhandled exception error has occurred"

Are you familiar with .NET's underlying principal of throwing Exceptions when something goes wrong? If you aren't, then stop where you are right now and read up on it. If you don't understand exceptions then you have no way to understand what the .NET framework components are doing when they raise an Exception.

You are trying to connect, but doing nothing to confirm that you ahve connected, or that the connection wasn't lost. You're just assuming that everything is perfect and that your .Connect command on line 6 worked. What if it didn't because there was no text in textBox1 for example?

Let me also throw in a couple tips:
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.
  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.
    btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)
  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like Index, TimeOut, Row, Column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
  • Try to avoid having work actually take place in GUI control event handlers. It is usually better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
    btnSave(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    SaveMenuItem(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    SaveContextMenu(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    Form1_Closing(object sender, eventargs e)
    {
        if (IsDirty) SavePreferences();
    }
    


I've seen plenty of vague error messages, but this sure isn't one of them. I'm not sure how much more clear this could be.

error details said:

A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied


The socket wasn't connected and no address was supplied. I don't think anyone can explain it any more precisely.

This post has been edited by tlhIn`toq: 25 June 2011 - 02:18 PM

Was This Post Helpful? 0
  • +
  • -

#3 morsev  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 70
  • Joined: 26-September 10

Re: simple question in socket programming

Posted 25 June 2011 - 02:22 PM

The textbox1 is not empty.
i write on it "localhost".
and the connection goes right.
but the problem appearing on the send command;
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6074
  • View blog
  • Posts: 23,540
  • Joined: 23-August 08

Re: simple question in socket programming

Posted 25 June 2011 - 11:08 PM

IPEndPoint iep = new IPEndPoint(IPAddress.Any, 100);
            socket.Bind(iep);
            socket.Listen(4);
            socket.Accept();
            Console.WriteLine("we get the connection");
            string s = "samer";
            byte[] buffer = Encoding.ASCII.GetBytes(s);
            socket.Send(buffer);

Read the manual on Socket.Accept(). The Accept method returns the socket that you should be writing to; you should NOT be writing to the same socket on which you accepted the connection.

Maybe reading this tutorial will also help.
Was This Post Helpful? 1
  • +
  • -

#5 morsev  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 70
  • Joined: 26-September 10

Re: simple question in socket programming

Posted 26 June 2011 - 01:12 AM

Thanks Jack the tutorial was very benefit for me .
now i understand what you have said about Accept function. :> :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1