A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.

VC# 2008 Express - Winsock Class

1.3k views · started by Chris ·
#1
VC# 2008 Express - Winsock Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MSWinsockLib;

namespace [color=Red][namespace][/namespace][/color]
{
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"
#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