Sunday, September 30, 2012

Basic Socket Programming in C#

using System;
using System.Text;/* this is server side code to receive data from client and sent data to client*/
using System.Net;
using System.Net.Sockets;
public class serv {
    public static void Main() {
    try {
        IPAddress ipAd = IPAddress.Parse("172.21.5.99");//this ip is server pc local ip
       
        
/* Initializes the Listener */
        TcpListener myList=new TcpListener(ipAd,8001);

/* Start Listeneting at the specified port */       
        myList.Start();
       
        Console.WriteLine("The server is running at port 8001...");   
        Console.WriteLine("The local End point is  :" +
                          myList.LocalEndpoint );
        Console.WriteLine("Waiting for a connection.....");
       
        Socket s=myList.AcceptSocket();
        Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
       
        byte[] b=new byte[100];
        int k=s.Receive(b);
        Console.WriteLine("Recieved...");
        for (int i=0;i<k;i++)
            Console.Write(Convert.ToChar(b[i]));

        ASCIIEncoding asen=new ASCIIEncoding();
/*sent data to client */
        s.Send(asen.GetBytes("The string was recieved by the server."));
        Console.WriteLine("\nSent Acknowledgement");
/* clean up */           
        s.Close();
        myList.Stop();
           
    }
    catch (Exception e) {
        Console.WriteLine("Error..... " + e.StackTrace);
    }   
    }
   
}


Client Side code  to sent data to server

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;
 
 
public class clnt {
 
    public static void Main() {
        
        try {
            TcpClient tcpclnt = new TcpClient();
            Console.WriteLine("Connecting.....");
            
            tcpclnt.Connect("172.21.5.99",8001);//this ip adress is server pc ip adress
            
            
            Console.WriteLine("Connected");
            Console.Write("Enter the string to be transmitted : ");
            
            String str=Console.ReadLine();
            Stream stm = tcpclnt.GetStream();
                        
            ASCIIEncoding asen= new ASCIIEncoding();
            byte[] ba=asen.GetBytes(str);
            Console.WriteLine("Transmitting.....");
            
            stm.Write(ba,0,ba.Length);//sent data to server
            
            byte[] bb=new byte[100];
            int k=stm.Read(bb,0,100);//receive data from server
            
            for (int i=0;i<k;i++)
                Console.Write(Convert.ToChar(bb[i]));
            
            tcpclnt.Close();
        }
        
        catch (Exception e) {
            Console.WriteLine("Error..... " + e.StackTrace);
        }
    }
 
}
 

Greetings from Coding World

Welcome to the Coding World. Feel free to your coding. Post your problem and get tremendous solution quickly!!!!!!!.

                                                                                                                                     

 Happy Coding
                                                                                                                          Mohammad Tanvirul Islam 
                                                                                                                                                         Android, ,C#, Flex  developer