socket in c#
C#
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
public class GetSocket
{
private static Socket ConnectSocket(string server, int port)
{
Socket s = null;
IPHostEntry hostEntry = null;
// Get host related information.
hostEntry = Dns.GetHostEntry(server);
// Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
// an exception that occurs when the host IP Address is not compatible with the address family
// (typical in the IPv6 case).
foreach(IPAddress address in hostEntry.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, port);
Socket tempSocket =
new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
tempSocket.Connect(ipe);
if(tempSocket.Connected)
{
s = tempSocket;
break;
}
else
{
continue;
}
}
return s;
}
// This method requests the home page content for the specified server.
private static string SocketSendReceive(string server, int port)
{
string request = "GET / HTTP/1.1\r\nHost: " + server +
"\r\nConnection: Close\r\n\r\n";
Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
Byte[] bytesReceived = new Byte[256];
string page = "";
// Create a socket connection with the specified server and port.
using(Socket s = ConnectSocket(server, port)) {
if (s == null)
return ("Connection failed");
// Send request to the server.
s.Send(bytesSent, bytesSent.Length, 0);
// Receive the server home page content.
int bytes = 0;
page = "Default HTML page on " + server + ":\r\n";
// The following will block until the page is transmitted.
do {
bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes);
}
while (bytes > 0);
}
return page;
}
public static void Main(string[] args)
{
string host;
int port = 80;
if (args.Length == 0)
// If no server name is passed as argument to this program,
// use the current host name as the default.
host = Dns.GetHostName();
else
host = args[0];
string result = SocketSendReceive(host, port);
Console.WriteLine(result);
}
}
Also in C#:
- Title
- foreach as parallel c#
- Category
- C#
- Title
- array syntax c#
- Category
- C#
- Title
- c# string array
- Category
- C#
- Title
- c# enum default
- Category
- C#
- Title
- c# generate random number
- Category
- C#
- Title
- c# get time
- Category
- C#
- Title
- c# how to use inovke
- Category
- C#
- Title
- make a list c#
- Category
- C#
- Title
- what function is called just before the a script is ended c#
- Category
- C#
- Title
- c# round number up
- Category
- C#
- Title
- c# making a folder
- Category
- C#
- Title
- exit game unity
- Category
- C#
- Title
- generate a dropdown list from array data using razor .net mvc
- Category
- C#
- Title
- how to change player cursor c# script unity
- Category
- C#
- Title
- c# get list of all class fields
- Category
- C#
- Title
- error CS0542
- Category
- C#
- Title
- c# list.foreach
- Category
- C#
- Title
- rotatearound unity
- Category
- C#
- Title
- how to get the last element in an array in c#
- Category
- C#
- Title
- unity collider2d contains point
- Category
- C#
- Title
- rotate player unity
- Category
- C#
- Title
- string to enum c#
- Category
- C#
- Title
- use newtonsoft json to clone object
- Category
- C#
- Title
- c# get executable path
- Category
- C#
- Title
- C# public 2d array
- Category
- C#
- Title
- isdaylightsavingtime in c#
- Category
- C#
- Title
- c# fileupload example
- Category
- C#
- Title
- install .net sdk ubuntu 20
- Category
- C#
- Title
- c# param exception
- Category
- C#
- Title
- how to clear console through script unity
- Category
- C#
- Title
- c# cheat sheet
- Category
- C#
- Title
- Request.ServerVariables["HTTP_X_FORWARDED_FOR"] get only one ipaddress
- Category
- C#
- Title
- c# how to run external program with args
- Category
- C#
- Title
- c# get enum value from string
- Category
- C#
- Title
- c# window instantly close
- Category
- C#
- Title
- C# for
- Category
- C#
- Title
- c# expression func automatically select return type
- Category
- C#
- Title
- open file in explorer c#
- Category
- C#
- Title
- optional parameter get request c#
- Category
- C#
- Title
- set current date to textbox in asp.net
- Category
- C#
- Title
- c# foreach namevaluecollection
- Category
- C#
- Title
- call Textboxfor in cs
- Category
- C#
- Title
- Celsius to Fahrenheit c#
- Category
- C#
- Title
- inheritance c#
- Category
- C#
- Title
- return random from enum
- Category
- C#
- Title
- c# stop loop in method
- Category
- C#
- Title
- how to edit a c# list
- Category
- C#
- Title
- countdownevent
- Category
- C#
- Title
- create dropdown in datatable c# dynamically
- Category
- C#
- Title
- value is null to insert in c#
- Category
- C#
- Title
- c# data types
- Category
- C#
- Title
- c# change variable types
- Category
- C#
- Title
- c# authorize attribute
- Category
- C#
- Title
- how to get array of children transform
- Category
- C#
- Title
- unity c# write line
- Category
- C#
- Title
- winforms timer c#
- Category
- C#
- Title
- c# index in select
- Category
- C#
- Title
- how to crouch in unity
- Category
- C#
- Title
- unity set material color
- Category
- C#
- Title
- how to say or in c#
- Category
- C#
- Title
- check which activity in focus in android
- Category
- C#
- Title
- wpf c# select folder path
- Category
- C#
- Title
- c# max two values
- Category
- C#
- Title
- blazor display validation message
- Category
- C#
- Title
- c# create dynamic object
- Category
- C#
- Title
- converting letter to ascii
- Category
- C#
- Title
- c# interview questions
- Category
- C#
- Title
- sort c#
- Category
- C#
- Title
- unity onclick addlistener
- Category
- C#
- Title
- c# remove spaces from string
- Category
- C#
- Title
- c# get country code
- Category
- C#
- Title
- c# input integer
- Category
- C#
- Title
- south african id number validation c#
- Category
- C#
- Title
- delegate function c#
- Category
- C#
- Title
- unity how to end a game with esc
- Category
- C#
- Title
- math class C# exponents
- Category
- C#
- Title
- gfortran: declare an array
- Category
- C#
- Title
- block wapalyzer from detecting codeigniter
- Category
- C#
- Title
- wpf binding ancestor codebehind
- Category
- C#
- Title
- find mongodb c# with task T
- Category
- C#
- Title
- c# discord bot
- Category
- C#
- Title
- C# webclient submit form
- Category
- C#
- Title
- how to change text to bold through script unity
- Category
- C#
- Title
- c# check if argument null
- Category
- C#
- Title
- ihttpactionresult to object c#
- Category
- C#
- Title
- c# serialize
- Category
- C#
- Title
- c# char array to string
- Category
- C#
- Title
- .net core get image from url
- Category
- C#
- Title
- c# groupby date
- Category
- C#
- Title
- C# previous method
- Category
- C#
- Title
- c# windows application get current path
- Category
- C#
- Title
- c# split a string and return list
- Category
- C#
- Title
- mongodb c# batch find
- Category
- C#
- Title
- asp.net core timeout
- Category
- C#
- Title
- eager loading c#
- Category
- C#
- Title
- check if network is available c#
- Category
- C#
- Title
- string isnullorempty vs isnullorwhitespace
- Category
- C#
- Title
- c# prime factorization
- Category
- C#
- Title
- c# tab character
- Category
- C#
- Title
- convert string to decimal c#
- Category
- C#