Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Friends i am making a client server application but the problem is that i cannot send data from CLIENT to SERVER , but i can receive data from SERVER to CLIENT ... Below is mt client and Server code please help me to sort out my problem..



CLIENT CODE

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


namespace asynclient
{
    class Program
    {
        Socket sc;
        IPEndPoint iep;
        IPAddress ipd;
        byte[] arr = new byte[1024];
        public byte[] data = new byte[1024];
	public Program()
        {
            connect();
           
        }
        public void connect()
        {
            try
            {
                sc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                ipd = IPAddress.Parse("127.0.0.1");
                iep = new IPEndPoint(ipd, 9050);
                sc.BeginConnect(iep, new AsyncCallback(connected), sc);
		        
                Console.ReadKey();
            }
            catch (SocketException ex)
            {
                Console.WriteLine("Cannot Connect to the server....");
            }
        }
        public void connected(IAsyncResult iar)
        {
            Socket ts = (Socket)iar.AsyncState;
            try
            {
                ts.EndConnect(iar);
                ts.BeginReceive(data, 0, data.Length, SocketFlags.None,new AsyncCallback(ReceiveData), ts);
                Console.WriteLine("Successfully Connected to Server");
            }
            catch(SocketException ex)
            {
                Console.WriteLine("Unable to connect to the host....");
            }
        }
        public void ReceiveData(IAsyncResult iar)
        {
            Socket remote = (Socket)iar.AsyncState;
            int recv = remote.EndReceive(iar);
            string stringData = Encoding.ASCII.GetString(data, 0, recv);
            Console.WriteLine(stringData);    
        }
        public void sendata()
        {
            string sz = "Yahoo Data Sent.";
            byte[] arr = System.Text.Encoding.ASCII.GetBytes(sz);
            sc.BeginSend(arr,0,arr.Length,SocketFlags.None,new AsyncCallback(send),sc);

 
        }

        public static void send(IAsyncResult iar)
        {
            Socket tc = (Socket)iar.AsyncState;
            int send = tc.EndSend(iar);
        }
        static void Main(string[] args)
        {
            Program a1 = new Program();
            a1.sendata();

        }
    }
}




SERVER CODE
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace asynserver
{
    class Program
    {
        Socket sc;
        IPEndPoint iep;
        byte[] data = new byte[1024];

        public Program()
        {
            listen();
        }
        public void listen()
        {
            try
            {
                sc=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
                iep = new IPEndPoint(IPAddress.Any,9050);
                sc.Bind(iep);
                sc.Listen(5);
                sc.BeginAccept(new AsyncCallback(CallAccept),sc);
                Console.ReadKey();
            }
            catch(SocketException ex)
            {
                Console.WriteLine("Unable to Accept Connection");
            }
        }
        public void CallAccept(IAsyncResult iar)
        {
            Socket oldserver = (Socket)iar.AsyncState;
            Socket client = oldserver.EndAccept(iar);
            Console.WriteLine("Connected to: " + client.RemoteEndPoint.ToString());
            string stringData = "Welcome to my server";
            byte[] message1 = Encoding.ASCII.GetBytes(stringData);
            client.BeginSend(message1, 0, message1.Length, SocketFlags.None,
            new AsyncCallback(SendData), client);
            client.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(ReceiveData), client);

        }
    
        public void SendData(IAsyncResult iar)
        {
            Socket client = (Socket)iar.AsyncState;
            int sent = client.EndSend(iar);
            
           
        }
        public void ReceiveData(IAsyncResult iar)
        {
            Socket remote = (Socket)iar.AsyncState;
            int recv = remote.EndReceive(iar);
            string stringData = Encoding.ASCII.GetString(data, 0, recv);
            Console.WriteLine(stringData);
        }


        static void Main(string[] args)
        {
            Program c1 = new Program();
        }
    }
}
Posted
Comments
Richard MacCutchan 26-Oct-14 4:29am    
Message not valid - removed by me.
Jahanzaib Niazi 26-Oct-14 4:30am    
Both client and server are on same machine ....
Jahanzaib Niazi 26-Oct-14 5:30am    
my both client and server are on same computer...
Richard MacCutchan 26-Oct-14 7:55am    
OK, I have run some tests and that IP address should work. You need to provide some more information about exactly what happens when you run your programs.
Richard MacCutchan 26-Oct-14 8:08am    
I have run your code and it works fine, the message "Yahoo Data Sent." is received by the server and displayed on the console.

Client/Server connections are usually request/response meaning that the communication is initiated from one side and the other side answers, duplex communication is very hard to do.

Try my article here : WCF Killer[^]
 
Share this answer
 
Comments
Jahanzaib Niazi 26-Oct-14 5:28am    
No dear , we can both send and recieve....
Thanks for help , i self have solved my problem...

It was just a small problem i just cut and paste my Client Side "SendData" function statements into the "Connected" Function...And Every Thing is now working fine...
 
Share this answer
 

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