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.Inter Network,So cketType.S tream,Prot ocolType.T cp);
int servport = Int32.Parse("5024");
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse (server),s ervport);
client.Connect(serverEndPo int);
foreach (string dString in sArray)
{
byte[] clientbuffer = Encoding.ASCII.GetBytes(dS tring);
client.Send(clientbuffer,0 ,clientbuf fer.Length ,SocketFla gs.None);
}
}
catch(Exception clienterror)
{
MessageBox.Show(clienterro r.Message) ;
}
finally
{
client.Close();
}
}
Server (Recieving)
public void dataColl()
{
Socket server = null;
try
{
server = new Socket(AddressFamily.Inter Network,So cketType.S tream,Prot ocolType.T cp);
server.Bind(new IPEndPoint(IPAddress.Any,5 024));
server.Listen(5);
}
catch(SocketException se)
{
MessageBox.Show(se.ErrorCo de + ": " + se.Message);
Environment.Exit(se.ErrorC ode);
}
byte[] databuffer = new byte[buffersize];
for (;;)
{
Socket client = null;
try
{
client = server.Accept();
System.Windows.Forms.ListV iewItem main = new System.Windows.Forms.ListV iewItem(cl ient.Remot eEndPoint. ToString() );
string[] sArray = new string[7];
for(int i=0; i < sArray.Length; i++)
{
client.Receive(databuffer, 0,databuff er.Length, SocketFlag s.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(servererro r.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.
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.Inter
int servport = Int32.Parse("5024");
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse
client.Connect(serverEndPo
foreach (string dString in sArray)
{
byte[] clientbuffer = Encoding.ASCII.GetBytes(dS
client.Send(clientbuffer,0
}
}
catch(Exception clienterror)
{
MessageBox.Show(clienterro
}
finally
{
client.Close();
}
}
Server (Recieving)
public void dataColl()
{
Socket server = null;
try
{
server = new Socket(AddressFamily.Inter
server.Bind(new IPEndPoint(IPAddress.Any,5
server.Listen(5);
}
catch(SocketException se)
{
MessageBox.Show(se.ErrorCo
Environment.Exit(se.ErrorC
}
byte[] databuffer = new byte[buffersize];
for (;;)
{
Socket client = null;
try
{
client = server.Accept();
System.Windows.Forms.ListV
string[] sArray = new string[7];
for(int i=0; i < sArray.Length; i++)
{
client.Receive(databuffer,
sArray[i] = databuffer.ToString();
}
DataReader.Write(sArray[0]
}
catch(Exception servererror)
{
MessageBox.Show(servererro
}
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(buffe r, 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.
//**first read the size of the data
ReadData(socket, ref buffer, 4);
int dataSize = BitConverter.ToInt32(buffe
//**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);
int dataSize = ReadDataPro(client/*socket
ASKER
Ok, I'll give this a go. Thanks.
byte[] clientbuffer = Encoding.ASCII.GetBytes(dS
//***with this line
byte[] clientbuffer = Encoding.Unicode.GetBytes(
//***change this line
sArray[i] = databuffer.ToString();
//***with this line
sArray[i] = Encoding.Unicode.GetString