Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Remoting / December 2004

Tip: Looking for answers? Try searching our database.

Communication with sockets

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
nvishnu - 31 Dec 2004 16:59 GMT
Hi all,

I am new to sockets and the way .NET implements it. I am trying to develop a
small test app. where a client connects to a server application and server
and sends messages. The server displays it in a WinForm. I am using asynch.
socket calls to achieve that. I have two issues with this.

First, i can only display the first message sent to the server. How can i
set the server run in a loop to listen to the incoming messages. The server
code here is below.
public ChatServer()
 {

  System.Net.IPHostEntry ipHostEntry =
Dns.Resolve(System.Net.Dns.GetHostName());
  int port = FindUnusedPort(ipHostEntry.AddressList[0]);

  System.Net.IPEndPoint ipEndPoint = new
IPEndPoint(ipHostEntry.AddressList[0],port);
  listener = new
Socket(ipEndPoint.AddressFamily,SocketType.Stream,ProtocolType.Tcp);

  listener.Bind(ipEndPoint);
  listener.Listen(10);

  IAsyncResult ar = listener.BeginAccept(new
System.AsyncCallback(OnConnectRequst),listener);

 }
public void OnConnectRequst(IAsyncResult result)
 {
  try
  {
   listener = (Socket ) result.AsyncState;
   m_socClient = listener.EndAccept(result);
    IAsyncResult ar =
m_socClient.BeginReceive(m_DataBuffer,0,m_DataBuffer.Length,SocketFlags.None
,new AsyncCallback(OnRecieve),m_socClient);
  }
  catch (SocketException e)
  {
   Console.WriteLine( e.Message);
  }
 }
 public void OnMessageRecieved(string message)
 {
  this.messageRecieved(message);
 }
 public void OnRecieve(IAsyncResult result)
 {
  m_socClient = (Socket) result.AsyncState;
  int iRx = 0;
  iRx = m_socClient.EndReceive(result);
  if (iRx>1)
  {
   string message = Encoding.ASCII.GetString(m_DataBuffer);
   this.OnMessageRecieved(message);
  }
 }

Second, The client sends a message and the server recieves it. I close the
client application and then reopen it connect to server and send message.
But the server doesn't recieves it.

Navin
Ken Kolda - 31 Dec 2004 18:51 GMT
In your OnReceive() method you need to call BeginReceive() on the socket
again to receive any additional data, e.g.

public void OnRecieve(IAsyncResult result)
{
  m_socClient = (Socket) result.AsyncState;
  int iRx = 0;
  iRx = m_socClient.EndReceive(result);
  if (iRx>1)
  {
   string message = Encoding.ASCII.GetString(m_DataBuffer);
   this.OnMessageRecieved(message);
   m_socClient.BeginReceive(m_DataBuffer,0,m_DataBuffer.Length,SocketFlags.None,new
AsyncCallback(OnRecieve),m_socClient);
  }
}

That said, you have a number of issues withi this code. For example, with
your current implementation, you'll only be able to accept a single client.
That's because you don't call BeginAccept() again in your OnConnectRequest()
method. Also, you're using a variable named m_Buffer for your receive
buffer, which is presumably a class variable. Since your server will
potentially be receiving data from multiple clients in different threads at
the same time, you have to have a different buffer per socket.

I'd advise looking at sample code to see how this is done because you should
certainly be able to find lots of examples. Just google "asynchronous
sockets .NET sample code".

Ken

> Hi all,
>
[quoted text clipped - 64 lines]
>
> Navin

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.