C # Socket Programming (a)

Since learning programming, all from the Internet search information that they grow a lot, suddenly thinking about their own summary or sentiment also impress it, there is this first post as a start.

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.Sockets;
using System.Net;

namespace SocketTest
{
   public partial class Form1 : Form
{
String SocketIP = "127.0.0.1";
int SocketPort = 6000;
static Socket ClientSocket;
public Form1()
{
   InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
ClientSocket.Send(Encoding.ASCII.GetBytes(textBox1.Text.ToString().Trim()));
}

void the Form1_Load Private (SENDER Object, EventArgs E)
{
the try
{
IPAddress IP = IPAddress.Parse (SocketIP); // convert the string to the IP address IPAddress instance
ClientSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream , ProtocolType.Tcp ); // with the specified address cluster protocol, a communication protocol and socket type
IPEndPoint endPoint = new IPEndPoint (ip, SocketPort); // with the specified port number and ip initialization IPEndPoint example
ClientSocket.Connect (endPoint); // establish a connection with the remote host

textBox2.Text = "starts transmitting the message";

byte [] message = Encoding.ASCII.GetBytes ( " Connect the Server"); // actually transmitted when the communication is a byte array, so to send a message byte conversion
ClientSocket.Send (Message);
TextBox2.Text = the textBox2 .Text + "\\ n" + "to send a message:" + Encoding.ASCII.GetString (message);

byte // [] = the receive new new byte [1024];
// int length = ClientSocket.Receive (the receive); // length received byte array length
//Console.WriteLine ( "the received message is:" + Encoding.ASCII. the GetString (the receive));
//ClientSocket.Close (); // close the connection
}
the catch (Exception EX)
{
TextBox2.Text ex.Message.ToString = ();
}
}
}
}

Guess you like

Origin www.cnblogs.com/merfhey/p/11345094.html