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

本文介绍了一个使用套接字进行图像文件发送和接收的实现方法,并提出了在发送图像的同时如何附带消息通知的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

my code is successfully sending and receiving 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 receiving 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.AppSettings["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

转载于:https://www.cnblogs.com/lixinsheng/archive/2009/05/26/1490342.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值