i have written the code to transfer image file through
sockets. Below is the code for client. It just runs and
exit nothing happens. Can anybody help?
In this code first i get the file size then the actual
file through bytes.
using System;
using System.Net.Sock ets;
using System.IO;
using System.Threadin g;
namespace ImageClient
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
bool flag = true;
Socket clientSocket;
AsyncCallback pnfCallback = null;
int fileSize;
byte[] buffer = new byte[1024];
/// <summary>
/// The main entry point for the
application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Class1 c = new Class1();
c.clientSocket = new Socket
(AddressFamily. InterNetwork,So cketType.Stream ,ProtocolType
..Tcp);
System.Net.IPEn dPoint ipend = new
System.Net.IPEn dPoint(System.N et.IPAddress.Pa rse(args
[0]),8221);
c.clientSocket. Connect(ipend);
c.WaitForData() ;
}
public void WaitForData()
{
if(pnfCallback == null)
{
pnfCallback = new
AsyncCallback(o nDataReceived);
}
IAsyncResult result =
clientSocket.Be ginReceive
(buffer,0,buffe r.Length,Socket Flags.None,pnfC allback,null)
;
}
public void onDataReceived( IAsyncResult
result)
{
if(flag)
{
int count =
clientSocket.En dReceive(result );
char[] chars = new char
[count];
System.Text.Enc oding.Default.G etDecoder().Get Chars
(buffer,0,count ,chars,0);
fileSize = int.Parse(new
string(chars));
Console.WriteLi ne("Got
the buffer " + fileSize);
Console.ReadLin e();
flag=false;
buffer = null;
buffer = new byte
[fileSize];
WaitForData();
}
else
{
clientSocket.En dReceive
(result);
FileStream fs = new
FileStream("c:/def.gif",FileMo de.Create);
fs.Write
(buffer,0,buffe r.Length);
fs.Flush();
fs.Close();
WaitForData();
}
}
}
}
sockets. Below is the code for client. It just runs and
exit nothing happens. Can anybody help?
In this code first i get the file size then the actual
file through bytes.
using System;
using System.Net.Sock ets;
using System.IO;
using System.Threadin g;
namespace ImageClient
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
bool flag = true;
Socket clientSocket;
AsyncCallback pnfCallback = null;
int fileSize;
byte[] buffer = new byte[1024];
/// <summary>
/// The main entry point for the
application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Class1 c = new Class1();
c.clientSocket = new Socket
(AddressFamily. InterNetwork,So cketType.Stream ,ProtocolType
..Tcp);
System.Net.IPEn dPoint ipend = new
System.Net.IPEn dPoint(System.N et.IPAddress.Pa rse(args
[0]),8221);
c.clientSocket. Connect(ipend);
c.WaitForData() ;
}
public void WaitForData()
{
if(pnfCallback == null)
{
pnfCallback = new
AsyncCallback(o nDataReceived);
}
IAsyncResult result =
clientSocket.Be ginReceive
(buffer,0,buffe r.Length,Socket Flags.None,pnfC allback,null)
;
}
public void onDataReceived( IAsyncResult
result)
{
if(flag)
{
int count =
clientSocket.En dReceive(result );
char[] chars = new char
[count];
System.Text.Enc oding.Default.G etDecoder().Get Chars
(buffer,0,count ,chars,0);
fileSize = int.Parse(new
string(chars));
Console.WriteLi ne("Got
the buffer " + fileSize);
Console.ReadLin e();
flag=false;
buffer = null;
buffer = new byte
[fileSize];
WaitForData();
}
else
{
clientSocket.En dReceive
(result);
FileStream fs = new
FileStream("c:/def.gif",FileMo de.Create);
fs.Write
(buffer,0,buffe r.Length);
fs.Flush();
fs.Close();
WaitForData();
}
}
}
}
Comment