Server Side

If you have understood whatever I have described so far, you will easily understand the Server part of the socket application. So far we have been talking about a client making connection to a server and sending and receiving data.

On the Server end, the application has to send and receive data. But in addition to adding and receiving data, server has to allow the clients to make connections by listening at some port. Server does not need to know client I.P. addresses. It really does not care where the client is because its not the server but client who is responsible for making connection. Server's responsibility is to manage client connections.

On the server side there has to be one socket called the Listener socket that listens at a specific port number for client connections. When the client makes a connection, the server needs to accept the connection and then in order for the server to send and receive data from that connected client it needs to talk to that client through the socket that it got when it accepted the connection. The following code illustrates how server listens to the connections and accepts the connection:

public Socket m_socListener;
public void
StartListening()
{
    try
    {
        //create the listening
socket...
        m_socListener = new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
     
  IPEndPoint ipLocal = new IPEndPoint ( IPAddress.Any ,8221);
        //bind
to local IP Address...
        m_socListener.Bind( ipLocal );
       
//start listening...
        m_socListener.Listen (4);
        // create
the call back for any client connections...
       
m_socListener.BeginAccept(new AsyncCallback ( OnClientConnect ),null);
     
  cmdListen.Enabled = false;
    }
    catch(SocketException se)
   
{
        MessageBox.Show ( se.Message );
    }
}

If you look at the above code carefully you will see that its similar to we
did in the asynchronous client. First of all the we need to create a listening
socket and bind it to a local IP address. Note that we have given Any as the
IPAddress (I will explain what it means later), and we have passed the port
number as 8221. Next we made a call to Listen function. The 4 is a
parameter indicating backlog indicating the maximum length of the queue of
pending connections.

Next we made a call to BeginAccept passing it a delegate
callback. BeginAccept is a non-blocking method that returns
immediately and when a client has made requested a connection, the callback
routine is called and you can accept the connection by calling
EndAccept. The EndAccept returns a socket object which
represents the incoming connection. Here is the code for the callback
delegate:

public void OnClientConnect(IAsyncResult asyn)
{
   
try
    {
        m_socWorker = m_socListener.EndAccept (asyn);
     
  WaitForData(m_socWorker);
    }
    catch(ObjectDisposedException)
 
  {
        System.Diagnostics.Debugger.Log(0,"1","\n OnClientConnection:
Socket has been closed\n");
    }
    catch(SocketException se)
   
{
        MessageBox.Show ( se.Message );
    }
}

Here we accept the connection and call WaitForData which in turn
calls BeginReceive for the m_socWorker.

If we want to send data some data to client we use m_socWorker socket for
that purpose like this:

Object objData = txtDataTx.Text;
byte[] byData =
System.Text.Encoding.ASCII.GetBytes(objData.ToString ());
m_socWorker.Send
(byData);

Socket Programming in C#--Server Side的更多相关文章

  1. C Socket Programming for Linux with a Server and Client Example Code

    Typically two processes communicate with each other on a single system through one of the following ...

  2. [PHP-Socket] Socket Programming in PHP

    Simple Client-Server socket program in PHP Introduction Sockets are used for interprocess communicat ...

  3. Socket Programming in C#--Conclusion

    Conclusion And that's all there is to it! Here is how our client looks like Here is how our server l ...

  4. Socket Programming in C#--Getting Started

    Getting Started You can argue that one can overcome these shortcomings by multithreading meaning tha ...

  5. How To: Perl TCP / UDP Socket Programming using IO::Socket::INET

    http://www.thegeekstuff.com/2010/07/perl-tcp-udp-socket-programming/ In this article, let us discuss ...

  6. Socket programming in C on Linux | tutorial

    TCP/IP socket programming This is a quick guide/tutorial to learning socket programming in C languag ...

  7. Python Socket Programming

    本文介绍使用Python进行Socket网络编程,假设读者已经具备了基本的网络编程知识和Python的基本语法知识,本文中的代码如果没有说明则都是运行在Python 3.4下. Python的sock ...

  8. linux c socket programming

    原文:linux c socket programming http://54min.com/post/http-client-examples-using-c.html 好文章 PPT http:/ ...

  9. TCP Socket Programming in Node.js

    TCP Socket Programming in Node.js Posted on October 26th, 2011 under Node.jsTags: Client, node.js, S ...

随机推荐

  1. 基本完成了一个SEGY扫描程序

    利用Seismic.NET编写了一个SEG-Y文件的扫描程序,可以自动判断道头字中主测线号.横测线号.X坐标和Y坐标的位置,自动快速扫描地震数据体中的拐点坐标.10GB多的数据体几十秒全部扫描完成! ...

  2. 微信公众平台SDK Python

    微信公众平台SDK 项目背景 从2014年开始玩微信公众平台,试用过其中大多数的功能,如:消息回复.自定义菜单.公众号中的支付,页面授权等.之前的程序中都是直接调用公众平台的接口,这样复用功能无法实现 ...

  3. Swift 2.0初探

    转眼间,Swift已经一岁多了,这门新鲜.语法时尚.类型安全.执行速度更快的语言已经渐渐的深入广大开发者的心. 今年6月,一年一度的WWDC大会如期而至,在大会上Apple发布了Swift 2.0,引 ...

  4. 剔除editor编辑器中的HTML标签

    1.剔除editor编辑器中的HTML标签 public static string striphtml(string strhtml)    {        string stroutput = ...

  5. SQL2014内存表性能之内存中 OLTP 的性能改进测试

    先贴1个例子,后续补充完整的测试例子.... 1.用MSDN例子测试一下 use master go --1.先创建包含内存优化文件组的数据库 CREATE DATABASE imoltp2 ON P ...

  6. High Aavialability with Group Replication-by宋利兵

    今天,2016年12月12日, Group Replication GA啦!GA的版本是MySQL-5.7.17. 下载连接如下: http://dev.mysql.com/downloads/mys ...

  7. 将Session写入Memcache

    通过session_set_save_handler()方法自定义Session写入Memcache <?php class MemSession{ private static $handle ...

  8. 烂泥:ubuntu安装vmtools

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 最近由于工作需要,需要使用桌面版的linux系统,所以就选择了ubuntu.同时为了方便使用,就在VM中安装ubuntu. 但是为了文件以及操作的方便就 ...

  9. 树形dp--hdu 3534 Tree

    Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  10. Linux环境安装MQ

    MQ下载地址:http://www-03.ibm.com/software/products/us/en/wmq/ 安装的MQ软件包为WMQv600Trial-x86_linux_2.tar.gz.  ...