L
low_pointer
Guest
hi, below is the code for my client-server chatting app for windows. All works fine but now i have to send smileys (png images) over sockets. and the following code is sending first smiley but then its throwing an exception as parameter is not valid at line : pictureBox4.Image = System.Drawing.Image.FromStream(mstream);
please help. Also can u suggest any other code snippet to send images over UDP. I have done it by converting image to byte array ->sent through UDP sockets. -> conversion to again into image. any other suggestion are welcome.. thank in advance..!!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Runtime.Serialization.Formatters.Binary;
namespace Chat_Chor
{
public partial class Form1 : Form
{
Socket sckt;
EndPoint epLocal, epRemote;
byte[] buffer;
public Form1()
{
InitializeComponent();
txtChat.Text = "";
}
private void Form1_Load(object sender, EventArgs e)
{
/* Thread t = new Thread(new ThreadStart(picsend));
t.IsBackground.CompareTo(true);
t.Start();
*/
//setting up the socket
sckt = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sckt.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
//getting users IP
txtLocalIp.Text = GetIPLocal();
//listView1.Items.Add(@"D:\My Works\awesome APPS\stickers\icon.png");
}
private string GetIPLocal()
{
IPHostEntry host;
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
if (ip.AddressFamily == AddressFamily.InterNetwork)
return ip.ToString();
return "127.0.0.1";
}
/* private string GetIPRemote()
{
IPEndPoint host;
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
if (ip.AddressFamily == AddressFamily.InterNetwork)
return ip.ToString();
return "127.0.0.1";
}*/
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void buttonConnect_Click(object sender, EventArgs e)
{
if (buttonConnect.Text != "Disconnect !")
{
//binding socket to the Button
epLocal = new IPEndPoint(IPAddress.Parse(txtLocalIp.Text), Convert.ToInt32(txtLocalPort.Text));
sckt.Bind(epLocal);
//connecting to Remote IP
epRemote = new IPEndPoint(IPAddress.Parse(txtRemoteIp.Text), Convert.ToInt32(txtRemotePort.Text));
sckt.Connect(epRemote);
listView1.Columns.Add("",0, HorizontalAlignment.Left);
listView1.Columns.Add("starting chat . . .", 444, HorizontalAlignment.Left);
//listening to (receiving from) the specific port on Remote side
buffer = new byte[2000];
sckt.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
buttonConnect.Text = "Disconnect !";
}
else
{
txtRemoteIp.Text = "";
buttonConnect.Text = "Connect";
}
}
private void MessageCallBack(IAsyncResult aResult)
{
try
{
if (buttonConnect.Text == "Disconnect !")
{
byte[] receivedData = new byte[2000];
receivedData = (byte[])aResult.AsyncState;
byte[] imgg = (byte[])aResult.AsyncState;
//converting byte[] to string
ASCIIEncoding aEncoding = new ASCIIEncoding();
string receivedMessage = aEncoding.GetString(receivedData).TrimEnd(\x0);
Console.Beep();
Thread.Sleep(1000);
string[] arr = new string[2];
ListViewItem itm;
arr[0] = "";
MemoryStream mstream = new MemoryStream(imgg);
pictureBox4.Image = System.Drawing.Image.FromStream(mstream);
string firstString = receivedMessage;
if (firstString.Length > 70)
{
firstString = firstString.Remove(70);
string renewedString = receivedMessage;
renewedString = renewedString.Remove(0, 70);
if (renewedString.Length > 70)
{
string secondstring = renewedString.Remove(70);
renewedString = renewedString.Remove(0, 70);
if (renewedString.Length <= 70)
{
arr[1] = "Friend : " + firstString + "-";
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
arr[1] = secondstring;
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
arr[1] = renewedString;
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
}
else
{
MessageBox.Show("message too long, reduce size !");
}
}
else
{
arr[1] = "Friend : " + firstString + "-";
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
arr[1] = renewedString;
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
}
}
else
{
arr[1] = "Friend : " + firstString;
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
}
//callbacking again
buffer = new byte[2000];
sckt.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
}
}
catch(Exception ex)
{
MessageBox.Show("ERROR : " + ex.ToString());
}
}
private void buttonSend_Click(object sender, EventArgs e)
{
try
{
if (buttonConnect.Text == "Disconnect !")
{
//convert string message to byte[]
ASCIIEncoding aEncoding = new ASCIIEncoding();
byte[] sendData = new byte[2000];
sendData = aEncoding.GetBytes(txtChat.Text);
//sending the Encoded message
/* byte[]picarr=new byte[50000];
MemoryStream ms = new MemoryStream(picarr);
ByteConverter bc = new ByteConverter();
*/
ImageConverter ic=new ImageConverter();
byte[] imgg = (byte[])ic.ConvertTo(pictureBox2.Image,typeof(byte[]));
// if (imgg == null)
// pictureBox2.Image = null;
//else
//{
// MemoryStream mstream = new MemoryStream(imgg);
// pictureBox1.Image = System.Drawing.Image.FromStream(mstream);
//}
sckt.Send(imgg);
sckt.Send(sendData);
string[] arr = new string[2];
ListViewItem itm;
arr[0] = "";
MemoryStream mstream = new MemoryStream(imgg);
pictureBox4.Image = System.Drawing.Image.FromStream(mstream);
string firstString = txtChat.Text;
if (firstString.Length > 70)
{
firstString = firstString.Remove(70);
string renewedString = txtChat.Text;
renewedString = renewedString.Remove(0, 70);
if (renewedString.Length > 70)
{
string secondstring = renewedString.Remove(70);
renewedString = renewedString.Remove(0, 70);
if (renewedString.Length <= 70)
{
arr[1] = "Me : " + firstString;
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
arr[1] = secondstring;
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
arr[1] = renewedString;
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
}
// else
//{
// MessageBox.Show("message too long, reduce size !");
//}
}
else
{
arr[1] = " Me : " + firstString + "-";
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
arr[1] = renewedString;
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
}
}
else
{
arr[1] = "Me : " + firstString;
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
}
txtChat.Text = "";
}
else
{
MessageBox.Show("Sorry, Session Expired !");
}
}
catch (Exception ex)
{
MessageBox.Show("ERROR : " + ex.ToString());
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void pictureBox2_Click(object sender, EventArgs e)
{
}
private void pictureBox3_Click(object sender, EventArgs e)
{
}
private void listView2_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView2.SelectedItems.Count >= 1)
{
ListViewItem lvi = listView2.SelectedItems[0];
pictureBox2.Image = lvi.ImageList.Images[lvi.ImageIndex];
}
}
private void listView2_VisibleChanged(object sender, EventArgs e)
{
}
}
}
Continue reading...
please help. Also can u suggest any other code snippet to send images over UDP. I have done it by converting image to byte array ->sent through UDP sockets. -> conversion to again into image. any other suggestion are welcome.. thank in advance..!!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Runtime.Serialization.Formatters.Binary;
namespace Chat_Chor
{
public partial class Form1 : Form
{
Socket sckt;
EndPoint epLocal, epRemote;
byte[] buffer;
public Form1()
{
InitializeComponent();
txtChat.Text = "";
}
private void Form1_Load(object sender, EventArgs e)
{
/* Thread t = new Thread(new ThreadStart(picsend));
t.IsBackground.CompareTo(true);
t.Start();
*/
//setting up the socket
sckt = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sckt.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
//getting users IP
txtLocalIp.Text = GetIPLocal();
//listView1.Items.Add(@"D:\My Works\awesome APPS\stickers\icon.png");
}
private string GetIPLocal()
{
IPHostEntry host;
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
if (ip.AddressFamily == AddressFamily.InterNetwork)
return ip.ToString();
return "127.0.0.1";
}
/* private string GetIPRemote()
{
IPEndPoint host;
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
if (ip.AddressFamily == AddressFamily.InterNetwork)
return ip.ToString();
return "127.0.0.1";
}*/
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void buttonConnect_Click(object sender, EventArgs e)
{
if (buttonConnect.Text != "Disconnect !")
{
//binding socket to the Button
epLocal = new IPEndPoint(IPAddress.Parse(txtLocalIp.Text), Convert.ToInt32(txtLocalPort.Text));
sckt.Bind(epLocal);
//connecting to Remote IP
epRemote = new IPEndPoint(IPAddress.Parse(txtRemoteIp.Text), Convert.ToInt32(txtRemotePort.Text));
sckt.Connect(epRemote);
listView1.Columns.Add("",0, HorizontalAlignment.Left);
listView1.Columns.Add("starting chat . . .", 444, HorizontalAlignment.Left);
//listening to (receiving from) the specific port on Remote side
buffer = new byte[2000];
sckt.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
buttonConnect.Text = "Disconnect !";
}
else
{
txtRemoteIp.Text = "";
buttonConnect.Text = "Connect";
}
}
private void MessageCallBack(IAsyncResult aResult)
{
try
{
if (buttonConnect.Text == "Disconnect !")
{
byte[] receivedData = new byte[2000];
receivedData = (byte[])aResult.AsyncState;
byte[] imgg = (byte[])aResult.AsyncState;
//converting byte[] to string
ASCIIEncoding aEncoding = new ASCIIEncoding();
string receivedMessage = aEncoding.GetString(receivedData).TrimEnd(\x0);
Console.Beep();
Thread.Sleep(1000);
string[] arr = new string[2];
ListViewItem itm;
arr[0] = "";
MemoryStream mstream = new MemoryStream(imgg);
pictureBox4.Image = System.Drawing.Image.FromStream(mstream);
string firstString = receivedMessage;
if (firstString.Length > 70)
{
firstString = firstString.Remove(70);
string renewedString = receivedMessage;
renewedString = renewedString.Remove(0, 70);
if (renewedString.Length > 70)
{
string secondstring = renewedString.Remove(70);
renewedString = renewedString.Remove(0, 70);
if (renewedString.Length <= 70)
{
arr[1] = "Friend : " + firstString + "-";
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
arr[1] = secondstring;
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
arr[1] = renewedString;
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
}
else
{
MessageBox.Show("message too long, reduce size !");
}
}
else
{
arr[1] = "Friend : " + firstString + "-";
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
arr[1] = renewedString;
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
}
}
else
{
arr[1] = "Friend : " + firstString;
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
}
//callbacking again
buffer = new byte[2000];
sckt.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
}
}
catch(Exception ex)
{
MessageBox.Show("ERROR : " + ex.ToString());
}
}
private void buttonSend_Click(object sender, EventArgs e)
{
try
{
if (buttonConnect.Text == "Disconnect !")
{
//convert string message to byte[]
ASCIIEncoding aEncoding = new ASCIIEncoding();
byte[] sendData = new byte[2000];
sendData = aEncoding.GetBytes(txtChat.Text);
//sending the Encoded message
/* byte[]picarr=new byte[50000];
MemoryStream ms = new MemoryStream(picarr);
ByteConverter bc = new ByteConverter();
*/
ImageConverter ic=new ImageConverter();
byte[] imgg = (byte[])ic.ConvertTo(pictureBox2.Image,typeof(byte[]));
// if (imgg == null)
// pictureBox2.Image = null;
//else
//{
// MemoryStream mstream = new MemoryStream(imgg);
// pictureBox1.Image = System.Drawing.Image.FromStream(mstream);
//}
sckt.Send(imgg);
sckt.Send(sendData);
string[] arr = new string[2];
ListViewItem itm;
arr[0] = "";
MemoryStream mstream = new MemoryStream(imgg);
pictureBox4.Image = System.Drawing.Image.FromStream(mstream);
string firstString = txtChat.Text;
if (firstString.Length > 70)
{
firstString = firstString.Remove(70);
string renewedString = txtChat.Text;
renewedString = renewedString.Remove(0, 70);
if (renewedString.Length > 70)
{
string secondstring = renewedString.Remove(70);
renewedString = renewedString.Remove(0, 70);
if (renewedString.Length <= 70)
{
arr[1] = "Me : " + firstString;
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
arr[1] = secondstring;
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
arr[1] = renewedString;
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
}
// else
//{
// MessageBox.Show("message too long, reduce size !");
//}
}
else
{
arr[1] = " Me : " + firstString + "-";
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
arr[1] = renewedString;
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
}
}
else
{
arr[1] = "Me : " + firstString;
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
}
txtChat.Text = "";
}
else
{
MessageBox.Show("Sorry, Session Expired !");
}
}
catch (Exception ex)
{
MessageBox.Show("ERROR : " + ex.ToString());
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void pictureBox2_Click(object sender, EventArgs e)
{
}
private void pictureBox3_Click(object sender, EventArgs e)
{
}
private void listView2_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView2.SelectedItems.Count >= 1)
{
ListViewItem lvi = listView2.SelectedItems[0];
pictureBox2.Image = lvi.ImageList.Images[lvi.ImageIndex];
}
}
private void listView2_VisibleChanged(object sender, EventArgs e)
{
}
}
}
Continue reading...