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;
namespaceOOSys
{
classClient
{
staticvoidMain(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.
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.
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 !"); }
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;
namespaceOOSysServer
{
classServer
{
staticvoidMain(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);
}
}
}
krishna v30 pointsPosted Feb 9, 2011, 6:19 PM EST
byte[] ProductData = System.Text.Encoding.ASCII.GetBytes(userInputString);
override .Tostring() and try sending the string data and then implement a binary formatter.
Ciwan6 pointsPosted Mar 10, 2011, 5:04 AM EST
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;
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); } } }