H
Hasok
Guest
Hey,
i'm trying to get into Sockets and im struggeling with sending multiple images.
Server:
public static void SendImage(int clientID)
{
TcpClient client = clientList[clientID];
NetworkStream nwStream = client.GetStream();
//---write back the text to the client---
byte[] bytesToSend2 = ASCIIEncoding.ASCII.GetBytes("Sending Image");
Console.WriteLine("Sending Image Message");
nwStream.Write(bytesToSend2, 0, bytesToSend2.Length);
Bitmap img = new Bitmap("mainchar.jpg");
byte[] bytesToSend = ImageToByte(img);
nwStream.Write(bytesToSend, 0, bytesToSend.Length);
nwStream.Dispose();
Console.WriteLine("Image sent");
}
public static void SendMessage(int clientID,String message)
{
TcpClient client = clientList[clientID];
NetworkStream nwStream = client.GetStream();
byte[] bytesToSend2 = ASCIIEncoding.ASCII.GetBytes("Sending Text " + message);
Console.WriteLine("Sending Image Message " + message);
nwStream.Write(bytesToSend2, 0, bytesToSend2.Length);
}
Client:
class Program
{
const int PORT_NO = 8081;
const string SERVER_IP = "127.0.0.1";
static void Main(string[] args)
{
Console.WriteLine("Enter id:");
String ergebnis = Console.ReadLine();
//---create a TCPClient object at the IP and port no.---
TcpClient client = new TcpClient(SERVER_IP, PORT_NO);
NetworkStream nwStream = client.GetStream();
byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(ergebnis);
//---send the text---
Console.WriteLine("Sending : " + ergebnis);
nwStream.Write(bytesToSend, 0, bytesToSend.Length);
//---read back the text---
byte[] bytesToRead = new byte[client.ReceiveBufferSize];
while (true)
{
if (nwStream.DataAvailable)
{
int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
Console.WriteLine("Received : " + Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));
if (Encoding.ASCII.GetString(bytesToRead, 0, bytesRead).Contains("Image"))
{
Console.WriteLine();
Image img = Image.FromStream(nwStream);
img.Save($"newimage.png", ImageFormat.Png);
}
}
}
}
Sending a textmessage multiple times is no problem at all but the sending of an Image only works once.
Once i send the image textmessaging is not possible anymore either and the line
NetworkStream nwStream = client.GetStream();
is throwing a InvalidOperationException. I ran out of ideas how to fix this.
Thanks in advance
Jan
Continue reading...
i'm trying to get into Sockets and im struggeling with sending multiple images.
Server:
public static void SendImage(int clientID)
{
TcpClient client = clientList[clientID];
NetworkStream nwStream = client.GetStream();
//---write back the text to the client---
byte[] bytesToSend2 = ASCIIEncoding.ASCII.GetBytes("Sending Image");
Console.WriteLine("Sending Image Message");
nwStream.Write(bytesToSend2, 0, bytesToSend2.Length);
Bitmap img = new Bitmap("mainchar.jpg");
byte[] bytesToSend = ImageToByte(img);
nwStream.Write(bytesToSend, 0, bytesToSend.Length);
nwStream.Dispose();
Console.WriteLine("Image sent");
}
public static void SendMessage(int clientID,String message)
{
TcpClient client = clientList[clientID];
NetworkStream nwStream = client.GetStream();
byte[] bytesToSend2 = ASCIIEncoding.ASCII.GetBytes("Sending Text " + message);
Console.WriteLine("Sending Image Message " + message);
nwStream.Write(bytesToSend2, 0, bytesToSend2.Length);
}
Client:
class Program
{
const int PORT_NO = 8081;
const string SERVER_IP = "127.0.0.1";
static void Main(string[] args)
{
Console.WriteLine("Enter id:");
String ergebnis = Console.ReadLine();
//---create a TCPClient object at the IP and port no.---
TcpClient client = new TcpClient(SERVER_IP, PORT_NO);
NetworkStream nwStream = client.GetStream();
byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(ergebnis);
//---send the text---
Console.WriteLine("Sending : " + ergebnis);
nwStream.Write(bytesToSend, 0, bytesToSend.Length);
//---read back the text---
byte[] bytesToRead = new byte[client.ReceiveBufferSize];
while (true)
{
if (nwStream.DataAvailable)
{
int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
Console.WriteLine("Received : " + Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));
if (Encoding.ASCII.GetString(bytesToRead, 0, bytesRead).Contains("Image"))
{
Console.WriteLine();
Image img = Image.FromStream(nwStream);
img.Save($"newimage.png", ImageFormat.Png);
}
}
}
}
Sending a textmessage multiple times is no problem at all but the sending of an Image only works once.
Once i send the image textmessaging is not possible anymore either and the line
NetworkStream nwStream = client.GetStream();
is throwing a InvalidOperationException. I ran out of ideas how to fix this.
Thanks in advance
Jan
Continue reading...