5 Replies - 614 Views - Last Post: 01 May 2014 - 09:44 AM Rate Topic: -----

#1 123456789igor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-October 13

sockets client+server with await/async c# 5.0

Posted 30 April 2014 - 06:24 PM

Hello, i try to write a simple asynchronous client/server apps. I know how do it using callback, but i want to invistigate how write client+server using await/async and c# 5.0. Please, help me. It should be simple. Only connect and transfer some bytes.
I know that this can be done using Tasks.
Something like this:
public static Task ConnectAsync(this Socket socket, EndPoint remoteEP)
        {
            return Task.Factory.FromAsync(socket.BeginConnect, socket.EndConnect, remoteEP, null);
        }

I try to find some examples, but this has no effect.
FYI. without tcplistener and NetworkStream.

Is This A Good Question/Topic? 0
  • +

Replies To: sockets client+server with await/async c# 5.0

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,698
  • Joined: 05-May 12

Re: sockets client+server with await/async c# 5.0

Posted 30 April 2014 - 07:48 PM

It looks like you are on the right track based on the information in MSDN: TPL and Traditional .NET Framework Asynchronous Programming

What problem are you running into (other than it looks you currently don't have enough parameters)?
Was This Post Helpful? 0
  • +
  • -

#3 123456789igor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-October 13

Re: sockets client+server with await/async c# 5.0

Posted 01 May 2014 - 05:27 AM

View PostSkydiver, on 30 April 2014 - 07:48 PM, said:

It looks like you are on the right track based on the information in MSDN: TPL and Traditional .NET Framework Asynchronous Programming

What problem are you running into (other than it looks you currently don't have enough parameters)?

i want to use c#5 and task.factory. If i understand correctly old methods beginXXX endXXX we can "wrap" into task.faktory and then run task. It's a lot easier. But ay this time i don't understand fully how do this
Was This Post Helpful? 0
  • +
  • -

#4 123456789igor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-October 13

Re: sockets client+server with await/async c# 5.0

Posted 01 May 2014 - 05:57 AM

Connect success. But how to send bytes, i don't know

private readonly Socket _server;
        public ServerSocket(IPAddress ipAddress, int port)
        {
            _server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _server.Bind(new IPEndPoint(ipAddress, port));
            _server.Listen(20);
            Console.WriteLine("Wait connection");
            Accept();
        }
        private async void Accept()
        {
            for (; ; )
            {
                var socket = await Task.Factory.FromAsync<Socket>(_server.BeginAccept, _server.EndAccept, true);
                Console.WriteLine("Connected");
            }
        }

This post has been edited by Skydiver: 01 May 2014 - 06:00 AM
Reason for edit:: Fixed content inside code. No need to do HTML escaping.

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,698
  • Joined: 05-May 12

Re: sockets client+server with await/async c# 5.0

Posted 01 May 2014 - 06:13 AM

I'm not seeing the issue... You do the exact same thing using the appropriate flavor of FromAsync() that lets you pass in the parameters. What am i missing? Or is this a "gimme the codez" question where you want us to do research for you and just give you the lines of code that you can paste into your code?
Was This Post Helpful? 0
  • +
  • -

#6 123456789igor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-October 13

Re: sockets client+server with await/async c# 5.0

Posted 01 May 2014 - 09:44 AM

now server can send to client array of byte. But client recieved only 1 iteration. How i can do, that if client connected then it always recieve information from server?
Server:

public ServerSocket(IPAddress ipAddress, int port)
        {
            _server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _server.Bind(new IPEndPoint(ipAddress, port));
            _server.Listen(20);
            Console.WriteLine("Wait connection");
            Accept();
        }
        private async Task StartSend(int i)
        {
            try
            {
                CodingImage img = new CodingImage();
                var buffer = img.CodingImages("C:\\image\\file"+i+".bmp");
                Task<int> senTask = SendAsync.AsyncSend(client, buffer, 0, buffer.Length);
            }
            catch(Exception ex)
            {

            }
        }
        private async void Accept()
        {
            for (int i = 0 ;i<14 ;i++ )
            {
                client = await Task.Factory.FromAsync<Socket>(_server.BeginAccept, _server.EndAccept, true);
                Console.WriteLine("Connected");
                await StartSend(i);

            }
        }

public static Task<int> AsyncSend(this Socket socket, byte[] buffer, int offset, int count)
        {
            return Task.Factory.FromAsync<int>(
                socket.BeginSend(buffer, offset, count, SocketFlags.None, null, socket),
                socket.EndSend);
        }



Client:

private async void Form1_Load(object sender, EventArgs e)
        {
            IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
            IPAddress ipAddress = ipHostInfo.AddressList[0];
            ClientSocket cs = new ClientSocket();
            await cs.Connect(ipAddress, 11000);
            //await cs.StartRecieve();
        }
public ClientSocket()
        {
            _client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        }
        public async Task Connect(IPAddress ipAddress, int port)
        {
            _client.Connect(ipAddress, port);
            if (!_client.Connected)
            {
                Console.WriteLine("Can't connect");
            }else
            Console.WriteLine("Connected");
            int i=0;
            while(i<14)
            {
                await StartRecieve(i);
                i++;
            }
        }
        public async Task StartRecieve(int i)
        {
            try
            {
                var buffer = new byte[999999];
                //Console.WriteLine("Картинка под номером {0} сохранена", i);
                buffer2 = await _client.ReceiveTaskAsync(buffer,0,buffer.Length);
                EncodingImage enc = new EncodingImage();
                enc.EncodingImageForRecievedBuffer(buffer, i);
                Console.WriteLine("Картинка под номером {0} сохранена", i);
            }
            catch (Exception ex)
            {
            }

}
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1