Forums - C# Corner

Forum guidelines
ahmed ahmed

ahmed ahmed

  • 1.3k
  • 2
  • 2.8k

how to send data to server using socket

Jul 16 2012 9:06 AM
i am trying to send data by soket but fail somthing like there is nothing server .
------------------------------------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;


    namespace server
    {
        class Program
        {
            static Socket socket;
            static byte[] buffer
            {
                get;
                set;
            }
            static void Main(string[] args)
            {

                //   i want recive data from client

                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Bind(new IPEndPoint(IPAddress.Parse("192.168.0.102"),1234));
                socket.Listen(100);

                Socket acepted = socket.Accept();
                buffer = new byte[acepted.SendBufferSize];
                int bytesRead = acepted.Receive(buffer);
                Byte[] formated = new Byte[bytesRead];

                for( int i =0; i < bytesRead  ;i++)
                {
                    formated[i] = buffer[i];
                }

                string strdata = Encoding.ASCII.GetString(formated);
                char[] cd = { ';' };
                string[] d = strdata.Split(cd);

                string first = d[0];
                string second = d[1];
                for (int i = 0; i < d.Length; i++)
                {
                    Console.Write(d[i]);
                }
                Console.Read();
                socket.Close();
                acepted.Close();

            }
        }
    }
    clint side
    ___________
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;

    namespace cliente
    {
        class Program
        {
            static Socket socket;

            static void Main(string[] args)
            {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint localendpoint = new IPEndPoint(IPAddress.Parse("192.168.0.102"), 1234);

                try
                {
                    socket.Connect(localendpoint);

                } catch
                {
                    Console.Write("unable");
                    Main(args);
                }

                Console.Write("enter somthing");
                String some = "i;want;send;data;toserver";
                byte[] data = Encoding.ASCII.GetBytes(some);
                socket.Send(data);
                Console.Read();
                socket.Close();

            }
        }
    }

    first i have started program server side then i start clint side but data not sent . plz  help me how to to do this ?

C# Corner Annual Conference 2017

SPONSORED BY

File APIs for .NET
Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!