Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.29/5 (4 votes)
See more:
I hav written the code for server to client transfer but unable to do the opposite...
Can any1 plz help me out asap

Server code
C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace ClientServerFileExchange
{
    class server
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Transfer a file");
                IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 1234);
                Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                sock.Bind(ipEnd);
                sock.Listen(100);
                Socket clientSock = sock.Accept();
                string fileName = "four.txt";// "Your File Name";
                string filePath = @"D:\";//Your File Path;
                byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName);
                byte[] fileData = File.ReadAllBytes(filePath + fileName);
                byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
                byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);
                fileNameLen.CopyTo(clientData, 0);
                fileNameByte.CopyTo(clientData, 4);
                fileData.CopyTo(clientData, 4 + fileNameByte.Length);
                clientSock.Send(clientData);
                Console.WriteLine("File:{0} has been sent.", fileName);
                clientSock.Close();
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("File Receiving fail." + ex.Message);
            }
            Console.Read();
        }
    }




Client code :

C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace client
{
    class client
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Program to transfer a file");
                IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234);
                Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                clientSock.Connect(ipEnd);

                byte[] clientData = new byte[1024 * 5000];
                string receivedPath = "D:/sovit/";
                int receivedBytesLen = clientSock.Receive(clientData);
                Console.WriteLine("received Bytes Len {0} ", receivedBytesLen);
                int fileNameLen = BitConverter.ToInt32(clientData, 0);
                Console.WriteLine("fileNameLen  {0}",fileNameLen);
                string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);
                Console.WriteLine("Connection Established\n & File started receiving : {0}", fileName);
                BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + fileName, FileMode.Append)); ;
                bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen);
                Console.WriteLine("File: {0} received & saved at path: {1}", fileName, receivedPath);
                bWrite.Close();
                clientSock.Close();
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("File Sending fail." + ex.Message);
            }
            Console.Read();
        }
    }
}
Posted
Updated 26-Jan-14 22:20pm
v3
Comments
Sergey Alexandrovich Kryukov 27-Jan-14 1:50am    
Not clear what's your problem. You just need to develop some application-level protocol which does exactly what you need...
—SA
ZurdoDev 27-Jan-14 13:08pm    
What's your question?
Szymon Roslowski 29-Jan-14 10:03am    
have a look in here:

http://www.codeproject.com/Articles/153938/A-Complete-TCP-Server-Client-Communication-and-RMI

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900