Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/session/session.php on line 423

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/session/session.php on line 423

Warning: Cannot modify header information - headers already sent by (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/session/session.php on line 426

Warning: Cannot modify header information - headers already sent by (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/templates/ja_purity/ja_templatetools.php on line 49
Socket Connect and Data Send in C# - The Scripts Library Community

Free Programming Tutorials & Source Code

 
  • Increase font size
  • Default font size
  • Decrease font size
Home C# Socket Connect and Data Send in C#

Socket Connect and Data Send in C#

E-mail
(2 votes, average: 5.00 out of 5)

Function to connect to host machine, and send data via TCP Socket.

Required Namespace:


using System.Net;
using System.Net.Sockets;

The function:


            public static void SendData(string data, Int32 port)
            {
        IPAddress host = IPAddress.Parse("127.0.0.1");// 
        IPEndPoint ipendpoint = new IPEndPoint(host, port ); // assign host and port
        // Note: port should be listening. To check that, open DOS command, type netstat -an and press Enter.
        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        try {
            socket.Connect(ipendpoint);
        } catch (SocketException e) {
            MessageBox.Show(e.Message);

            socket.Close();
            return;
        }
        try {
            socket.Send(Encoding.ASCII.GetBytes(data));
        } catch (SocketException e) {
            MessageBox.Show(e.Message);
            socket.Close();
            return;
        }
        socket.Close();
            }


That's it!