TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Ska
NA
5
0
Socket: Send and receive zipped files
Jan 26 2009 5:57 AM
Hi all,
I have just inherited some code that sends a file from server to client. However, the code only works for text files, and I'm trying to modify it so that it can send and receive zipped files too. But I'm stuck, and don't know how to go about changing it.
The current code looks like this:
//SERVER
private void SendFileToClient(SocketPacket state, string PFilename)
{
if (File.Exists(PFilename))
{
FileStream fileStream = File.Open(PFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
StreamReader reader = new StreamReader(fileStream);
byte[] sendBytes = Encoding.ASCII.GetBytes(reader.ReadToEnd());
MessageBox.Show(sendBytes.Length + "");
state.m_currentSocket.Send(sendBytes, 0, sendBytes.Length, SocketFlags.None);
fileStream.Close();
reader.Close();
}
}
//CLIENT
public void OnDataReceived(IAsyncResult asyn)
{
try
{
SocketPacket theSockId = (SocketPacket)asyn.AsyncState;
int iRx = theSockId.thisSocket.EndReceive(asyn);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
theSockId.message += szData;
if ((theSockId.message.Length > 0) && ((theSockId.message[0] != '<') ||
((theSockId.message[0] == '<') && (theSockId.message.IndexOf("</message>") > -1))))
{
DealWithResponse(theSockId);
}
else
WaitForData(theSockId);
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
public void DealWithResponse(SocketPacket theSockId)
{
if (theSockId.message != null)
{
FileStream fileStream = File.Open(theSockId.theGame.KeyFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
StreamWriter writer = new StreamWriter(fileStream);
writer.Write(theSockId.message);
writer.Close();
fileStream.Close();
}
}
Like I said before, this code works fine for text files. But it doesn't work for non-text files. What should I change to make it so that it works for both text and non-text files?
Let me know if you need more of the code. Thank you for taking your time to look at this.
Reply
Answers (
5
)
Performing date calculations using a calendar
Need to OCR a jpeg file in C# or VB (.net)