Hi All


I am trying to write Asynchronous client-Server Socket program in which whenever a client is connected to server a Windows Form is created which has a textbox and two buttons......on Data recieved by server from client the data is written in respective windows Form textbox and if a user on server side clicks a button in the Windows Form which is shown in Modal by ShowDialog method Data is sent from Server to respective client......


Let me Make things clear with a example Say there is two client conected to the server than any message from the client will be written on the respective Form.Let us assume here the two client as Jack and Jill.Now when Jack connects to server a new form pops in server ...Similarly when Jill connects to server another form pops in the server ...Now when ever Jack sends data to server data is written to jacks form on Server and when Jill sends data to server the data is written on Jills form on Server in this way the user on Server can know as to what Jack has written and Jill Has written in separate window.........


I have tried a lot for writtin the code for this part of my project...But no luck till now.....it has taken my night sleep and hence i am in forum...

Guys please help me.....Please provide me the code if you can or Guide me in this regard.....

Thanks in Advance

Recommended Answers

All 3 Replies

Please show some code and explain where you are having trouble and I will try to help.

Hi I found a work around by using threading Since i was unable to code in nonblocking way please go through the code and help me to use the socket asynchronously....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
using Microsoft.Win32;

namespace ComputerLabMangementProject
{
   public static  class ClassUsbRequest
    {
      
       public static void  Request()
       { //bool ButtonStatus=true;
           try
           {
               Socket ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
               // Set the remote IP address
               IPAddress ip = IPAddress.Parse(GetIP());
               int iPortNo = System.Convert.ToInt16(8000);
               // Create the end point 
               IPEndPoint ipEnd = new IPEndPoint(ip, iPortNo);
               // Connect to the remote host
               
               
                   FormUsbReason frmReason = new FormUsbReason();
                   DialogResult frmReasonResult = frmReason.ShowDialog();
                   if (frmReasonResult == DialogResult.Cancel)
                   {
                       //ClientSocket.Disconnect(false);
                       ClientSocket.Close();
                       frmReason.Dispose();
                       Program.frmLogin.frmLogout.changeEnable(true);

                   }
                   else
                   {
                       if (frmReason.Reason)
                       {
                           ClientSocket.Connect(ipEnd);
                           Object objData = Program.frmLogin.UserName + "%" + frmReason.TextBoxReason.Text;
                           byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString());
                           ClientSocket.Send(byData);
                           byte[] buffer = new byte[1024];
                           int iRx = ClientSocket.Receive(buffer);
                           char[] chars = new char[iRx + 1];
                           System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
                           int charLen = d.GetChars(buffer, 0, iRx, chars, 0);
                           System.String szData = new System.String(chars);

                           if (szData.ToString() == "yes\0")
                           {
                               MessageBox.Show("Put usb enable code here");
                               //this will enable pen Drive
                               //Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\USBSTOR", true).SetValue("Start", 3);
                               ClientSocket.Close();
                           }
                           else
                           {
                               if (szData == (String)"No\0")
                               {
                                   
                                   Program.frmLogin.frmLogout.changeEnable(true);
                                   MessageBox.Show("You Dont have Permission for use of pen Drive");
                                   ClientSocket.Close();
                               }
                           }
                       }
                       else
                       {
                           Program.frmLogin.frmLogout.changeEnable(true);
                       }

                   }
               }
           
           catch (Exception ex)
           {
               MessageBox.Show("Error In Conecting To Server Make Sure Server Is On And Lab Management Application Is Running/nERROR NO 051");
               LogAppend.Append(ex.ToString());
           }
            //return ButtonStatus;

       }
       	
       // This is a helper function used (for convenience) to 
       // get the IP address of the local machine
       
        static String GetIP()
       {
           String strHostName = Dns.GetHostName();

           // Find host by name
           IPHostEntry iphostentry = Dns.GetHostByName(strHostName);

           // Grab the first IP addresses
           String IPStr = "";
           foreach (IPAddress ipaddress in iphostentry.AddressList)
           {
               IPStr = ipaddress.ToString();
               return IPStr;
           }
           return IPStr;
       }		
       


    }
}

Hi I found a work around by using threading

I am not sure how your posted code uses threading as there is no threading code there.
Assuming that your client code calls the Request() method in a seperate thread to the main one, I do not think that blocking calls will give you a problem. It looks like you are on the right track with what you are doing.
That said, your server code will need to use the asynchronous calls to enable multiple user access.

The MSDN help for Socket.BeginAccept has some examples of using the asynchronous callbacks.
ocket.BeginAccept Method (Int32, AsyncCallback, Object)
Socket.BeginAccept Method (Socket, Int32, AsyncCallback, Object)

Have a go and if you have any specific questions start a new thread so you get others looking at your problem too.

Good luck with your project.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.