Click here to Skip to main content
15,042,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Actually i had developed client server one-way commucation.it's working fine after that

i had developed two-way communcation.i will explain one-way and two-way communcation

For suppose client send one Request(message) like "HI" then server listening the request

and it's shows "Hi" measge on server side then at the time server not responding client

request.it's only take user request show on server side only.this is one-way

communcation but whenever server responding for client request like "Hello" then called

two-way communcation.my project not working two-way communication.really tell u

minium it's not working one-way communcation(client request will not send to server)

in my project also.


clientside udp socket program code:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

namespace clientserver1
{
    class chart1
    {
        private static System.Threading.Thread clientThread;
        static void Main(string[] args)
        {
            CreateThreads();
        }
        private static void CreateThreads()
        {
            clientThread = new System.Threading.Thread(new
            System.Threading.ThreadStart(RunClientThread));
            clientThread.Start();
        }
        

        private static void RunClientThread()
        {
            string sendStr = "";
            UdpClient theClient = new UdpClient("192.168.0.5",9050);
            while (!sendStr.Trim().ToUpper().Equals("END"))
            {
                sendStr = Console.ReadLine();
                byte[] myData = new byte[1024];
                myData = Encoding.ASCII.GetBytes(sendStr);
                theClient.Send(myData, myData.Length);
            }
            theClient.Close();
        }

        private static void RunServerThread()
        {
            string rcvData = "";
            IPEndPoint IPEP = new IPEndPoint(IPAddress.Any, 9050);
            UdpClient theSock = new UdpClient(IPEP);
            IPEndPoint fromClient;
            while (!rcvData.Trim().ToUpper().Equals("END"))
            {
                byte[] myData = new byte[1024];
                fromClient = new IPEndPoint(IPAddress.Any, 0);
                myData = theSock.Receive(ref fromClient);
                rcvData = Encoding.ASCII.GetString(myData);
                Console.WriteLine(fromClient.ToString() + " " + rcvData);
            }
            theSock.Close();
        }

    }
}


serverside udp socket program code:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

namespace ClientServer_2
{
    class chart2
    {
        private static System.Threading.Thread clientThread;

        static void Main(string[] args)
        {
            CreateThreads();
        }
        private static void CreateThreads()
        {
            clientThread = new System.Threading.Thread(new
            System.Threading.ThreadStart(RunServerThread));
            clientThread.Start();
        }

        private static void RunClientThread()
        {
            string sendStr = "";
            UdpClient theClient = new UdpClient("192.168.0.5",9050);
            while (!sendStr.Trim().ToUpper().Equals("END"))
            {
                sendStr = Console.ReadLine();
                byte[] myData = new byte[1024];
                myData = Encoding.ASCII.GetBytes(sendStr);
                theClient.Send(myData, myData.Length);
            }
            theClient.Close();
        }

        private static void RunServerThread()
        {
            string rcvData = "";
            IPEndPoint IPEP = new IPEndPoint(IPAddress.Any, 9050);
            UdpClient theSock = new UdpClient(IPEP);
            IPEndPoint fromClient;
            while (!rcvData.Trim().ToUpper().Equals("END"))
            {
                byte[] myData = new byte[1024];
                fromClient = new IPEndPoint(IPAddress.Any, 0);
                myData = theSock.Receive(ref fromClient);
                rcvData = Encoding.ASCII.GetString(myData);
                Console.WriteLine(fromClient.ToString() + " " + rcvData);
            }
            theSock.Close();
        }
    }
}


above both applications are running same machine

clientside udp socket program and serverside udp socket program are same due to both

applications are runing on same machine.if the applications are running on different

machines then The source code will be identical except for IP address specified in the

RunClientThread( ) in serverside udp socket program.i think my code is correct.if any

wrong logic will be there pls modify the code.



if u want to any reference(not .dll) to the project .pls tell me ur mailid. i will

forward the client-server pdf.i had developed the project based on client-server pdf

can u please solve me for two-way communcation?


thank u.
Posted
Updated 19-Oct-15 9:22am
v3
Comments
Krishna Veni 19-Oct-15 15:48pm
   
https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=3&ved=0CC0QFjACahUKEwik4MPeo8_IAhXHC44KHY9UBUE&url=http%3A%2F%2Fcsclab.murraystate.edu%2Fbob.pilgrim%2F410%2FProject06.pdf&usg=AFQjCNH9PXyFBrxNT5oTXe4_ss-kuUEXOw&sig2=a5aMk-ySISBEBjRQ7pBFlQ&bvm=bv.105454873,d.c2E.
Krishna Veni 19-Oct-15 15:50pm
   
Above link is client-server pdf file link.pls download the pdf file.this will be very helpfull for project.if any doubts pls refer that pdf then easly you are sloving problem

1 solution

I recommend that you take a look at this information, freely available on MSDN.

Socket Code Examples[^]

There you can find code examples for both synchronous and asynchronous client and server.
In your case I guess the asynchronous example is best suited.
Instead of using your own thread you will use the methods BeginReceive and EndReceive, for example.

I have built applications on this code myself in the past. You always have to change the code to your needs, but it is a good example to start with.
   

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