send msg

  • Thread starter Thread starter Alakhim Mohamad
  • Start date Start date
A

Alakhim Mohamad

I'd like to send a message from server to client via socket with the
following code

...........server.........
string s="message";
Byte[] b=new Byte[1024];
b=ASCII.GetBytes(s);
socket.Send(b);
.............................

...........client...........

NetworkStream ns=client.getstream();
Byte[] b=new Byte[1024];
while((int i=ns.Read(b,0,b.Length)!=0)
{
string s=ASCII.GetString(b,0,i);
Console.Write("{0}",s);
}
...............................


This code is not correct right ? I see nothing on the client side at
all
Please help

Thanks
 
I post this to just tell you that I am very frustrated because I still
haven't found a solution yet, please help me
 
Try out this one.

client :
public static void StartSending()
{
Byte[] bytes = new Byte[1024];
Socket _sender = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
_sender.Connect(new IPEndPoint(new IPAddress(new Byte[] {
<IP>}), <port>));
try
{
data = "This is a test msg from client";
bytes = Encoding.ASCII.GetBytes(data);
int bytesRec = _sender.Send(bytes);
Console.WriteLine(bytesRec + " bytes Sent to the server.");

}
catch (Exception e)
{
Console.WriteLine("Exception occured:" + e.StackTrace);
}

}


server:
public static void StartListener(){

Byte[] bytes = new Byte[1024];
Socket _listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
_listener.Bind(new IPEndPoint(new IPAddress(new Byte[] { <IP>
}), <port>));
_listener.Listen(10);

try {
Socket _handler = _listener.Accept();
bytes = new Byte[1024];
int bytesRec = _handler.Receive(bytes);
Console.WriteLine(Encoding.ASCII.GetString(bytes, 0,
bytesRec));
}
catch (Exception e) {
Console.WriteLine("Exception occured:" + e.StackTrace);
}
}


Amit
 
Back
Top