How to send and receive image using socket programming in C #

Source: Internet
Author: User
My code is successfully sending and processing ing images using sockets .... but problem is that I want to send messages along images .... ie if client sends all images from a defined location... then client must send a message to tell server .... that now its server turn to send image... if I merge messsage along byte data of images then... on Loading ing end only image file is created but no previou... means images are not displayed ....
Code is placed below...

Public void receiveimage ()
{
Try
{
Sock. Listen (100 );
Socket clientsock = sock. Accept ();
Byte [] clientdata = new byte [1024*5000];
Int receivedbyteslen = clientsock. Receive (clientdata );
If (receivedbyteslen = 19)
{
String message = encoding. ASCII. getstring (clientdata, 0, 19 );
If (Message = "server_img_complete ")
{
Thread. Sleep (6000 );
Sendimage objfrm = new sendimage ();
Objfrm. getpatientdocuments ();
}
}
Else
{
Receivedpath = system. configuration. configurationsettings. receivettings ["receivedpath"]. tostring ();
Int filenamelen = bitconverter. toint32 (clientdata, 0 );
String filename = encoding. ASCII. getstring (clientdata, 4, filenamelen );
Binarywriter bwrite = new binarywriter (file. Open (receivedpath + "/" + filename, filemode. append ));;
Bwrite. Write (clientdata, 4 + filenamelen, receivedbyteslen-4-filenamelen );
DB. openconnection ();
String start_from = dB. getfielddescription ("ss_emr_image_sync_time_log", "Top 1 (date_to) as field_date", "acknowledged = 1 order by sr desc ");
Bwrite. Close ();
Clientsock. Close ();
}
Receiveimage ();
}

</Code>

Code for client application:

Class ftclientcode
{
Public static void sendfile (string filename)
{
Try
{
IPaddress [] IPaddress = DNS. gethostaddresses ("localhost ");
Ipendpoint ipend = new ipendpoint (IPaddress [0], 5656 );
Socket clientsock = new socket (addressfamily. InterNetwork, sockettype. Stream, protocoltype. IP );
String filepath = "";
Filename = filename. Replace ("\\","/");
While (filename. indexof ("/")>-1)
{
Filepath + = filename. substring (0, filename. indexof ("/") + 1 );
Filename = filename. substring (filename. indexof ("/") + 1 );
}
Byte [] filenamebyte = encoding. ASCII. getbytes (filename );
If (filenamebyte. length> 850*1024)
{
Curmsg = "file size is more than 850kb, please try with small file .";
Return;
}
// Byte [] clientdata = new byte [12 + filenamebyte. Length + filedata. Length];
Byte [] filenamelen = bitconverter. getbytes (filenamebyte. Length );
Byte [] Message = encoding. ASCII. getbytes ("image_ OK ");
Filenamelen. copyto (clientdata, 0 );
Filenamebyte. copyto (clientdata, 4 );
Filedata. copyto (clientdata, 4 + filenamebyte. Length );
Message. copyto (clientdata, filenamebyte. Length + filedata. Length );}
}
Clientsock. Connect (ipend );
Clientsock. Send (clientdata );
Clientsock. Close ();

awaitng for ur kind response.
thanks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.