Skip to content

Slightly Harder Socket Programming

Ciwan

Asked Feb 6, 2011, 5:10 AM EST2 answers
Share
LinkedInX
Hello Friends.

I have a Product class, and it is programmed to act as a data structure basically. Here is the code:

 
C#
using System;
namespace OOSys
{
class Product
{
private int ProductNo { get; set; }
private string ProductName { get; set; }
private string ProductType { get; set; }
private double ProductPrice { get; set; }
private string ProductMaker { get; set; }
// Constructor
public Product(int number, string name, string type, double price, string maker)
{
ProductNo = number;
ProductName = name;
ProductType = type;
ProductPrice = price;
ProductMaker = maker;
}
}
}


What I want to do is serialize a product, and send it via socket to another running program. I've called my two programs Client and Server. Here is the code for my Client class:

 
C#
using System;
using System.Net;
using System.Net.Sockets;
namespace OOSys
{
class Client
{
static void Main(string[] args)
{
try
{
Product p = new Product(1, "CoD", "Shooter", 39.99, "UbiSoft");
string userInputString;
Console.WriteLine("This program will transfer a product to a server. \n");
Console.WriteLine("Please type the number 1 followed by Enter to send the Product:");
userInputString = Console.ReadLine();
if (userInputString == "1")
{
Console.WriteLine("You Entered: " + userInputString);
Console.WriteLine("Status: Sending Product ... ...");
IPAddress[] ipAddress = Dns.GetHostAddresses("localhost");
IPEndPoint remoteEP = new IPEndPoint(ipAddress[0], 8221);
Socket socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
socClient.Bind(remoteEP);
socClient.Connect(remoteEP);
 
byte[] ProductData = System.Text.Encoding.ASCII.GetBytes(userInputString);
socClient.Send(ProductData);
socClient.Close();
Console.ReadLine();
}
else
{
Console.WriteLine("Nothing will be sent !");
}

}
catch (SocketException se)
{
Console.WriteLine(se);
}
}
}
}


 
Ignoring the Server side program for now (I'll move onto that in a bit) ... do you see anything wrong with the above code ?

I have read the articles on the right, but they were just general info, I couldn't see anything about sending Objects to a Server program.

I would greatly appreciate any help with this.

Thank you.

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    u r sending the user input string.not the product data.
    byte[] ProductData = System.Text.Encoding.ASCII.GetBytes(userInputString);
    override .Tostring() and try sending the string data and then implement a binary formatter.
    0
  • 0
    Hello Krishna

    Thanks for that, I went ahead and corrected that. Here is what my Client code looks like now:

    [code]public class Client   {
           static void Main(string[] args)
           {
               try
               {
                   Product p = new Product(1, "CoD", "Shooter", 39.99, "UbiSoft");


                   string userInputString;
                   Console.WriteLine("This program will transfer a product to a server. \n");
                   Console.WriteLine("Please type the number 1 followed by Enter to send the Product:");
                   userInputString = Console.ReadLine();
                   if (userInputString == "1")
                   {
                       Console.WriteLine("You Entered:  " + userInputString);
                       Console.WriteLine("Status: Sending Product ... ...");
                       IPAddress[] ipAddress = Dns.GetHostAddresses("localhost");
                       IPEndPoint remoteEP = new IPEndPoint(ipAddress[0], 8221);
                       Socket socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                       socClient.Bind(remoteEP);
                       socClient.Connect(remoteEP);


                       byte[] ProductData = p.Serialize(); //fills the buffer with data
                       socClient.Send(ProductData);


                       socClient.Close();
                       Console.ReadLine();


                   }
                   else
                   {
                       Console.WriteLine("Nothing will be sent !");
                   }
                   
               }
               catch (SocketException se)
               {
                   Console.WriteLine(se);
               }


           }
       }[/code]

    And here is the code for my Server class. All I want to do is send a Product object via a Socket between them. I can't believe how difficult this is (at least for me) :(

    [code]using System;
    C#
    using System.Net;
    using System.Net.Sockets;
    using System.IO;
    using OOSys;
    
    
    namespace OOSysServer
    {
        class Server
        {
            static void Main(string[] args)
            {
                try
                {
                    Console.WriteLine("This is the Server. It is waiting for a connection ...");
                    IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 8221);
                    Socket socServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
    
    
                    Product p = new Product();
    
    
                    while (true)
                    {
                        socServer.Bind(remoteEP);
                        socServer.Listen(100);
                        Socket socClient = socServer.Accept();
    
    
                        socClient.Close();
                        Console.ReadLine();
                    }
    
    
                    p.TransmissionBuffer.Add(p.buffer[i]);
    
    
                } catch (SocketException se) {
    
    
                    Console.WriteLine(se);
                }
            }
        }
    }[/code]
    0