Code:using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using MSWinsockLib; namespace [NAMESPACE] { public class Socket { private Winsock TCPWinsock; public Socket() { SocketCreate(-1); } public Socket(int Port) { SocketCreate(Port); } public void SocketCreate(int Port) { TCPWinsock = new Winsock(); TCPWinsock.Error += new DMSWinsockControlEvents_ErrorEventHandler(this.Error); TCPWinsock.ConnectionRequest += new DMSWinsockControlEvents_ConnectionRequestEventHandler(this.ConnectionRequest); TCPWinsock.DataArrival += new DMSWinsockControlEvents_DataArrivalEventHandler(this.DataArrival); TCPWinsock.SendComplete += new DMSWinsockControlEvents_SendCompleteEventHandler(this.SendComplete); TCPWinsock.SendProgress += new DMSWinsockControlEvents_SendProgressEventHandler(this.SendProgress); if (Port != -1) { TCPWinsock.LocalPort = Port; } } public void Listen() { if (TCPWinsock.State == 0) { TCPWinsock.Listen(); } } public void Connect(String IP, int Port) { if (TCPWinsock.State == 0) { TCPWinsock.RemoteHost = IP; TCPWinsock.RemotePort = Port; TCPWinsock.Connect(IP, Port); } } public int State() { return TCPWinsock.State; } #region Winsock Event Handlers public void Error(short Number, ref string Description, int Scode, string Source, string HelpFile, int HelpContext, ref bool CancelDisplay) { //Handle Error } public void ConnectionRequest(int requestID) { //Handle ConnectionRequest } public void DataArrival(int bytesTotal) { //Handle DataArrival } public void SendComplete() { //Handle SendComplete } public void SendProgress(int bytesSent, int bytesRemaining) { //Handle SendProgress } #endregion } }
Add Reference - COM "Microsoft Winsock Control 6.0"
Results 1 to 2 of 2
Thread: VC# 2008 Express - Winsock Class
- 30 Jan. 2010 09:06am #1
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 200.00
VC# 2008 Express - Winsock Class
- 30 Jan. 2010 10:27am #2
Remember to use closed ports that other programs aren't using. If you need to find closed ports just go here:
Online Port Scanner 2.0