Link to home
Start Free TrialLog in
Avatar of pab89
pab89

asked on

Data transfer between client and server

I'm trying to send a few strings of text from a client program to a server program, I am currently using the following code:

Client (Sending)

            public void netSend(string server,string category,string details,string computername,string username,string winversion)
            {
                  string[] sArray = {server, category, details, computername, username, winversion};
                  Socket client = null;
                  try
                  {
                        client = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
                        int servport = Int32.Parse("5024");
                        IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(server),servport);
                        client.Connect(serverEndPoint);
                        foreach (string dString in sArray)
                        {
                              byte[] clientbuffer = Encoding.ASCII.GetBytes(dString);
                              client.Send(clientbuffer,0,clientbuffer.Length,SocketFlags.None);
                        }
                  }
                  catch(Exception clienterror)
                  {
                        MessageBox.Show(clienterror.Message);
                  }
                  finally
                  {
                        client.Close();
                  }
            }

Server (Recieving)

            public void dataColl()
            
            {
                  Socket server = null;
                  try
                  {
                        server = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
                        server.Bind(new IPEndPoint(IPAddress.Any,5024));
                        server.Listen(5);
                  }
                  catch(SocketException se)
                  {
                        MessageBox.Show(se.ErrorCode + ": " + se.Message);
                        Environment.Exit(se.ErrorCode);
                  }
                  byte[] databuffer = new byte[buffersize];
                  for (;;)
                  {
                        Socket client = null;

                        try
                        {
                              client = server.Accept();
                              System.Windows.Forms.ListViewItem main = new System.Windows.Forms.ListViewItem(client.RemoteEndPoint.ToString());
                              string[] sArray = new string[7];
                                    for(int i=0; i < sArray.Length; i++)
                                    {
                                          client.Receive(databuffer,0,databuffer.Length,SocketFlags.None);
                                          sArray[i] = databuffer.ToString();
                                    }
                              DataReader.Write(sArray[0],sArray[1],sArray[2],sArray[3],sArray[4],sArray[5],sArray[6],sArray[7],"Yes",1,"");
                        }
                        catch(Exception servererror)
                        {
                              MessageBox.Show(servererror.Message);
                        }
                        finally
                        {
                              client.Close();
                        }
                  }
            }

(The buffersize is set with "private const int buffersize = 32;")

The text I recieve is garbled. I think the issue is timing related, the server code runs constantly in a separate thread. What am I doing wrong? Is it easier to use streamreader and streamwriter and what is packet framing?

Only 110 points...it's all I have, sorry.
Avatar of philoware
philoware

//***change this line
byte[] clientbuffer = Encoding.ASCII.GetBytes(dString);
//***with this line
byte[] clientbuffer = Encoding.Unicode.GetBytes(dString+"\0");

//***change this line
 sArray[i] = databuffer.ToString();
//***with this line
sArray[i] = Encoding.Unicode.GetString( databuffer).Split('\0')[0];
Avatar of pab89

ASKER

Better...but not quite. The strings are still being split in the wrong places. It's as if they're being put into the array before the whole string has been recieved. Is this something to do with the buffer size since I don't know how large the recieved strings will be.
ASKER CERTIFIED SOLUTION
Avatar of philoware
philoware

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pab89

ASKER

Looks good, upped the points to 115 :P Just one problem, this line doesn't compile:

                  //**first read the size of the data
                  ReadData(socket, ref buffer, 4);
                  int dataSize = BitConverter.ToInt32(buffer, 0);
                  //**then read the data
            --->>  ReadData(socket, buffer, dataSize);
                  return dataSize;

I could make it work by changing that to "ref buffer" but I'm not sure if that's entirely a good thing.
Yes, as you see it must be ref. I forgot it.
And this one must be ref, too.
int dataSize = ReadDataPro(client/*socket*/, ref dataBuffer);
Avatar of pab89

ASKER

Ok, I'll give this a go. Thanks.