0% found this document useful (0 votes)
24 views

Tcplistener: Objhttp Setrequestheader

The document describes code for creating a TCP listener server in C# that accepts client connections on port 13000, receives data from connected clients, converts it to uppercase and sends it back. It also contains code for removing the close button from a Windows form and handling web socket requests.

Uploaded by

Maria LoOpez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Tcplistener: Objhttp Setrequestheader

The document describes code for creating a TCP listener server in C# that accepts client connections on port 13000, receives data from connected clients, converts it to uppercase and sends it back. It also contains code for removing the close button from a Windows form and handling web socket requests.

Uploaded by

Maria LoOpez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.

0;
Windows NT 5.0)"

tcplistener

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

class MyTcpListener
{
public static void Main()
{
TcpListener server=null;
try
{
// Set the TcpListener on port 13000.
Int32 port = 13000;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");

// TcpListener server = new TcpListener(port);


server = new TcpListener(localAddr, port);

// Start listening for client requests.


server.Start();

// Buffer for reading data


Byte[] bytes = new Byte[256];
String data = null;

// Enter the listening loop.


while(true)
{
Console.Write("Waiting for a connection... ");

// Perform a blocking call to accept requests.


// You could also use server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");

data = null;

// Get a stream object for reading and writing


NetworkStream stream = client.GetStream();

int i;
// Loop to receive all the data sent by the client.
while((i = stream.Read(bytes, 0, bytes.Length))!=0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);

// Process the data sent by the client.


data = data.ToUpper();

byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

// Send back a response.


stream.Write(msg, 0, msg.Length);
Console.WriteLine("Sent: {0}", data);
}

// Shutdown and end connection


client.Close();
}
}
catch(SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
server.Stop();
}

Console.WriteLine("\nHit enter to continue...");


Console.Read();
}
}

if (new Regex("^GET").IsMatch(data)) {
Byte[] response = Encoding.UTF8.GetBytes("HTTP/1.1 101
Switching Protocols" + Environment.NewLine
+ "Connection: Upgrade" + Environment.NewLine
+ "Upgrade: websocket" + Environment.NewLine
+ "Sec-WebSocket-Accept: " + Convert.ToBase64String (
SHA1.Create().ComputeHash (
Encoding.UTF8.GetBytes (
new Regex("Sec-WebSocket-Key:
(.*)").Match(data).Groups[1].Value.Trim() + "258EAFA5-E914-47DA-
95CA-C5AB0DC85B11"
)
)
) + Environment.NewLine
+ Environment.NewLine);

stream.Write(response, 0, response.Length);
}

this.FormBorderStyle = FormBorderStyle.None;

then recreate the minimize and maximize buttons

private void button1_Click(object sender, EventArgs e)


{
this.WindowState = FormWindowState.Maximized;
}

private void button2_Click(object sender, EventArgs e)


{
this.WindowState = FormWindowState.Minimized;
}

Private Sub DisableCloseButton(ByVal hWnd As IntPtr)


Try 'captura de excepciones

Dim menuItemCount As IntPtr


Dim hMenu As IntPtr
'Obtener el manejador del menú de sistema del formulario
hMenu = GetSystemMenu(hWnd.ToInt32(), False)
'Obtener la cuenta de los ítems del menú de sistema.
'Es el menú que aparece al pulsar sobre el icono a la
izquierda
'de la Barra de título de la ventana, consta de los ítems:
Restaurar, Mover,
'Tamaño,Minimizar, Maximizar, Separador, Cerrar.
menuItemCount = GetMenuItemCount(hMenu.ToInt32())
'Quitar el ítem Close (Cerrar), que es el último de ese menú
RemoveMenu(hMenu.ToInt32(), menuItemCount.ToInt32() - 1,
MF_DISABLED Or MF_BYPOSITION)
'Quitar el ítem Separador, el penúltimo de ese menú, entre
Maximizar y Cerrar
RemoveMenu(hMenu.ToInt32(), menuItemCount.ToInt32() - 2,
MF_DISABLED Or MF_BYPOSITION)
'Redibujar la barra de menú
DrawMenuBar(hWnd.ToInt32())

'mostrar un mensaje con la excepción producida


Catch pollo As Exception
MessageBox.Show("Se ha producido la excepción: " + vbCrLf +
pollo.Message, _
"Error del programa", MessageBoxButtons.OK)
End Try
End Sub

ControlBox = False

You might also like