Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
Hi everyone, I have a problem with my socket code.
When I send "SEND" command from CLIENT, SERVER send image to CLIENT, its success.
How can I send / receive continuous images WHEN I send "SEND" command?
Please help me, thanks!

SERVER
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace server_image_3
{
    class Program
    {
        static void Main(string[] args)
        {
            IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            server.Bind(iep);
            server.Listen(10);
            Console.WriteLine("Waiting for connection...");
            Socket client = server.Accept();
            NetworkStream ns = new NetworkStream(client);
            StreamReader sr = new StreamReader(ns);
            StreamWriter sw = new StreamWriter(ns);
            //string kq = "";
            while (true)
            {
                string s = sr.ReadLine();
                s = s.ToUpper();
                if (s.Equals("QUIT"))
                {
                    break;
                }
                if (s.Equals("SEND"))
                {
                    //kq = DateTime.Now.ToString("dd/MM/yyyy");
                    // send the file
                    byte[] buffer = ReadImageFile("1.jpg");
                    client.Send(buffer, buffer.Length, SocketFlags.None);
                    Console.WriteLine("Send success!");
                }
                
                sw.Flush();
            }
            sr.Close();
            sw.Close();
            client.Close();
        }
        private static byte[] ReadImageFile(String img)
        {
            FileInfo fileinfo = new FileInfo(img);
            byte[] buf = new byte[fileinfo.Length];
            FileStream fs = new FileStream(img, FileMode.Open, FileAccess.Read);
            fs.Read(buf, 0, buf.Length);
            fs.Close();
            //fileInfo.Delete ();
            GC.ReRegisterForFinalize(fileinfo);
            GC.ReRegisterForFinalize(fs);
            return buf;
        }
    }
}

////////////////////
CLIENT
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace client_image_3
{
    class Program
    {
        static void Main(string[] args)
        {
            IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
            Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            client.Connect(iep);
            NetworkStream ns = new NetworkStream(client);
            StreamReader sr = new StreamReader(ns);
            StreamWriter sw = new StreamWriter(ns);
            while (true)
            {
                string input = Console.ReadLine();
                sw.WriteLine(input);
                sw.Flush();
                if (input.ToUpper().Equals("QUIT"))
                {
                    break;
                }
                else 
                {
                    // receive data
                    
                    
                        byte[] buffer = new byte[1000000];
                        client.Receive(buffer, buffer.Length, SocketFlags.None);
                        Console.WriteLine("Receive success");
                        
                        FileStream fs = File.Create("1.jpg");

                        fs.Write(buffer, 0, buffer.Length);
                        
                    
                    //fs.Close();
                }
                //
                //string kq = sr.ReadLine();
               // Console.WriteLine(kq);
            }
            sr.Close();
            sw.Close();
            ns.Close();
            client.Close();
        }
    }
}
Posted
Updated 28-Mar-17 11:29am
v2
Comments
[no name] 26-Aug-14 9:29am    
where is ur problem
Member 10390715 26-Aug-14 9:36am    
FileStream fs = File.Create("1.jpg");
fs.Write(buffer, 0, buffer.Length);

if i have this code, image will be save, but it send only one time because repeat name 1.jpg in folder. But if i don't have this code, it won't save image. How can i fix it?
Richard MacCutchan 26-Aug-14 10:08am    
you need to implement a protocol between the server and client, where each file is preceded by some infomation about its name and size. Then the client can read that information and it will know when it has received a complete file. It then starts reading the next file and so on, until the server sends a message to say there are no more files.
Member 10390715 26-Aug-14 10:27am    
thanks
Member 10390715 26-Aug-14 10:40am    
but I don't know, how to make it. Can you tell me?

1 solution

Why not just use "File" class?

Your server:

 class Program
{
    static void Main(string[] args)
    {
        IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
        Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        server.Bind(iep);
        server.Listen(10);
        Console.WriteLine("Waiting for connection...");
        using (Socket client = server.Accept())
        {
            while (true)
            {
                string s = Console.ReadLine().ToUpper();

                if (s.Equals("QUIT"))
                {
                    break;
                }
                if (s.Equals("SEND"))
                {
                    // send the file
                    byte[] buffer = File.ReadAllBytes("1.jpg");
                    client.Send(buffer, buffer.Length, SocketFlags.None);
                    Console.WriteLine("Send success!");
                }
            }
        }
    }
}


Your client:

C#
class Program
{
    static void Main(string[] args)
    {
        IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
        using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
        {
            client.Connect(iep);

            while (true)
            {
                string input = Console.ReadLine();

                if (input.ToUpper().Equals("QUIT"))
                {
                    break;
                }
                else
                {
                    // receive data
                    byte[] buffer = new byte[1000000];
                    client.Receive(buffer, buffer.Length, SocketFlags.None);
                    Console.WriteLine("Receive success");

                    File.WriteAllBytes("1.jpg", buffer);
                }
            }
        }
    }
}
 
Share this answer
 
Comments
Member 10390715 27-Aug-14 9:13am    
Thanks, I will try it.

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