Hi my name is vishal for past 10 days i have been breaking my head on how to create control array ofwinsock control in c#windows forms?
I got to use winsock control in c# windows forms through Toolbox->right click->choose items->COM Components->Microsoft WinSock Control,version 6.0.
I am using winsock control in c# windows forms in order to createclient and server program/client server communication. I have to usewinsock control in c# windows forms because of my BOSS's orders and that i cannot use classes related toSystem.Net for achieving the result.
I have included namespaces: using System.Net and using System.Net.Sockets in my program in c# windows forms.
I did a little research and with some help from a friend/a generous member of CODE PROJECT forum i was able to achieve the result of creating a control array of winsock control in c# windows forms. Given below is thec# code:
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.Diagnostics; namespace DRRS_Socket_Application { public partial class Form1 : Form { private AxMSWinsockLib.AxWinsock axWinsock1; private AxMSWinsockLib.AxWinsock[] sckClient = new AxMSWinsockLib.AxWinsock[20]; Socket sck; EndPoint epLocal; const string DELIM = "***"; const string EOP = "???"; int counter = 0; public Form1() { InitializeComponent(); sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); } private string GetLocalIP() { 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 void Form1_Load(object sender, EventArgs e) { int x; for (x = 0; x < 20; x++) { sckClient[x]=new AxMSWinsockLib.AxWinsock(); ((System.ComponentModel.ISupportInitialize)(this.axWinsock1)).BeginInit(); sckClient[x].Enabled = true; StartListen(); } } private void btnSend_Click(object sender, EventArgs e) { int x; for (x = 0; x < 20; x++) { byte[] abData; string Str; Str = textBox3.Text.ToString(); abData = System.Text.Encoding.Default.GetBytes(Str); sckClient[x].CreateControl(); sckClient[x].SendData(abData); } } private void StartListen() { sckServer.Close(); sckServer.LocalPort = 25000; sckServer.Listen(); }
private void axWinsock1_DataArrival(object sender, AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
{
Byte[] strData = new byte[30];
Object buffer = (Object)strData;
sckClient[0].GetData(ref buffer);
string Mstr="";
int i;
for (i = 0; i == strData.GetUpperBound(0); i++)
{
Mstr = Mstr + Convert.ToString(strData);
}
listBox1.Items.Add(Mstr);
}private void sckServer_ConnectionRequest_1(object sender, AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent e)
{
int x;
for (x = 0; x < 20; x++)
{
sckClient[x].CreateControl();
sckClient[x] = new AxMSWinsockLib.AxWinsock();
((System.ComponentModel.ISupportInitialize)(this.axWinsock1)).BeginInit();
if (axWinsock1.CtlState== 2)
{
//So close and accept them now
sckClient[x].Close();
sckClient[x].Accept(e.requestID);
sckClient[x].SendData("Connected");
//'They are already connected so no need in going on
}
}
}
where axWinsock1 is name of my winsock control and sckServer is name of my another winsock control inForm1 in c# windows forms.
In above code upon execution i get no compilation errors or run-time errors.
I did a little research and found out the code that worked the way i wanted but unfortunately it was invb6 code. Given below is vb6 code:
Option Explicit
Private Const DELIM As String = "***"
'Separates actual packets (stands for 'End of Packet').
Private Const EOP As String = "???"
Private intCounter As Integer
Private Sub Command3_Click()
Dim abData() As Byte
Dim Str As String
Dim i As Long
Str = Text3.Text
' Convert string to bytes
abData = StrConv(Str, vbFromUnicode)
sckClient(0).SendData abData
End Sub
Private Sub Form_Load()
Dim x As Integer
For x = 1 To 20 'This will allow a maximum of 20 clients to be connected at once
Load sckClient(x)
sckClient(x).Close
Next
StartListen
End Sub
Private Sub sckClient_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim strData() As Byte
sckClient(Index).GetData strData, vbByte, bytesTotal
Dim Mstr As String
Dim i As Long
For i = 0 To UBound(strData)
Mstr = Mstr & CStr(strData(i))
Next i
List1.AddItem Mstr
End Sub
Private Function StartListen()
sckServer.Close
sckServer.LocalPort = 25000
sckServer.Listen
End Function
Private Sub sckServer_ConnectionRequest(ByVal requestID As Long)
'They made a connection request so look for an open sckClient and accept them
Dim x As Integer
For x = 0 To 20
If Not sckClient(x).State = sckConnected Then
'So close and accept them now
sckClient(x).Close
sckClient(x).Accept requestID
sckClient(x).SendData "Connected"
'They are already connected so no need in going on
Exit For
End If
Next
End Subwhere sckServer and sckClient are name of my winsock controls in my form invb6.
The reason i am asking this question in this forum because even though i dont get compilation or run-time errors in my c# code. However after execution of abovevb6 code i am able to see sckClient(winsock control) displayed assckClient(0) on top of sckClient(winsock control) and name property:sckClient which i dont seem to understand of it is possible.
My next question is sending textbox data to server by sendData function of sckClient(0). Given below is vb6 code:
Private Sub Command3_Click()
Dim abData() As Byte
Dim Str As String
Dim i As Long
Str = Text3.Text
' Convert string to bytes
abData = StrConv(Str, vbFromUnicode)
sckClient(0).SendData abData
End SubThe above code executes and works exactly the way i wanted in vb6.
Given below is my translation/interpretation of above vb6 code inc# windows forms:
private void btnSend_Click(object sender, EventArgs e)
{
int x;
for (x = 0; x < 20; x++)
{
byte[] abData;
string Str;
Str = textBox3.Text.ToString();
abData = System.Text.Encoding.Default.GetBytes(Str);
sckClient[x].CreateControl();
sckClient[x].SendData(abData);
}
}In above c# code i am not able to send data to server from sckClient as upon entering data in textBox3 and when i clickbtnSend button i get error: Com Exception was unhandled:Exception from HRESULT:0x800A9C46 which points to following line in c#:
sckClient[x].SendData(abData);
My question is have i correctly created control array of winsock control named:sckClient and assigned to winsock control named:axWinsock1 in myForm1 in c# windows forms?
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.Diagnostics;
namespace DRRS_Socket_Application
{
public partial class Form1 : Form
{
private AxMSWinsockLib.AxWinsock axWinsock1;
private AxMSWinsockLib.AxWinsock[] sckClient = new AxMSWinsockLib.AxWinsock[20];
public Form1()
{
InitializeComponent();
sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
}
private void Form1_Load(object sender, EventArgs e)
{
int x;
for (x = 0; x < 20; x++)
{
sckClient[x]=new AxMSWinsockLib.AxWinsock();
((System.ComponentModel.ISupportInitialize)(this.axWinsock1)).BeginInit();
sckClient[x].Enabled = true;
StartListen();
}
}I am not questioning/doubting the help/knowledge shared/helped by one user from one forum but i am questioningmyself have i correctly created control array of winsock control inc# windows forms.?
My last question is if i have to create a control array of
winsock control in c# windows forms should i do it in viacode in c# and not worry about control at design time and only worry it's functionality and at run-time? If so can anyone help/guide me please!? Can anyone provide/share a sample with me?
I know/aware that i have asked 2 questions here. I am asking here because i dont have anyone to help/guide me to solve my problem.
Can anyone help me please?! Can anybody help/guide me on how to solve my problem? Any help/guidance in solving of this problem would be greatly appreciated!
vishal