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. android 浏览器开发实例

    android app需要通过手机显示网页信息还是比较常用的,比如我最近业余开发的 抢商铺游戏,需要对游戏规则做说明,规则会比较多,而且要经常变动,就想到用网页来展示,更新起来方便,不像应用,一旦发布 ...

  2. UnityShader之Shader格式篇【Shader资料1】

    关于Shader,在Unity里面我们一般叫做ShaderLab,只要你的职业是与渲染搭边,Unity就与ShaderLab有着直接的关联,你都应该试着去学会它,其实我们在新手未有入门的时候,我们总是 ...

  3. Android网络编程只局域网传输文件

    Android网络编程之局域网传输文件: 首先创建一个socket管理类,该类是传输文件的核心类,主要用来发送文件和接收文件 具体代码如下: package com.jiao.filesend; im ...

  4. iOS开发笔记3:XML/JSON数据解析

    这篇主要总结在iOS开发中XML/JSON数据解析过程用到的方法.XML数据解析主要使用SAX方式的NSXMLParser以及DOM方式的GDataXML,JSON数据解析主要使用NSJSONSeri ...

  5. Spring概述

    layout: post title: Spring概述 tags: [Java,Spring,IOC,AOP] --- Spring是一个开源的轻量级Java SE(Java 标准版本)/Java ...

  6. javascript的模块化解读

    AMD是RequireJS在推广过程中对模块定义的规范化产出. 异步加载模块,依赖前置,提前执行. Define定义模块 define(['require','foo'],function(){ret ...

  7. Effective Java 46 Prefer for-each loops to traditional for loops

    Prior to release 1.5, this was the preferred idiom for iterating over a collection: // No longer the ...

  8. 第八章 了解tempdb数据库

    1.一个sqlserver数据库实例上只能有一个tempdb数据库,这个实例上所有的用户都共享这个数据库.2.tempdb数据库在每次sqlserver重启后都会重新创建,所以数据会丢失.3.因为te ...

  9. Cocos2d-x解析XML文件,解决中文乱码

    身处大天朝,必须学会的一项技能就是解决中文显示问题.这个字符问题还搞了我一天,以下是个人解决乱码问题的实践结果,希望可以给其他人一些帮助 读取xml文件代码: CCDictionary* messag ...

  10. hbase常用命令总结

    创建表:表名:csliyb:testuser列族:name 例子:create 'csliyb:testuser','name','age' 添加记录: put 'csliyb:testuser',' ...