Saturday, October 20, 2012

Send File from Server to Client using C# Socket Programming 2/6

2) Client Action: Now turn is coming to client to request server.

IPAddress[] ipAddress = Dns.GetHostAddresses("localhost");

IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], 5656);

Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);


These codes are from client application, here first two lines are using to get Localhost IP address and by using this creating new IPEnd point. Be sure there IP address and port number must be same as server address. I am running my applications in same machine so using localhost.

Next two lines of code is creating a socket object and trying to connect by using IPEnd point. So this socket object will try to connect server socket which was in listen mode.

3) Server Action: Again turn comes to server about to response on client’s request and this is doing by below line of code in server application:

Socket clientSock = sock.Accept();

This “sock” is server socket object which was created previously and it was in listen mode. This sock object will accept client request and generate a new socket object with name “clientSock”. Rest all work in server side will be performed by “clientSock” object to handle this particular client request.

No comments: