Forum guidelines
AuthorQuestion
how to send data to server using socket
Posted By ahmed ahmed on 16 Jul 2012
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 ?

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.