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
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
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();
}
}
}