Asp.net Socket instance

Source: Internet
Author: User

 
Public class Socketclient {TextBox txt; TextBox send_txt; public Socketclient (TextBox txt, TextBox send_txt) {this.txt = txt; this. send_txt = send_txt;} public void StartClient () {// Data buffer for incoming data. byte [] bytes = new byte [1024]; // Connect to a remote device. try {// Establish the remote endpoint for the socket. // This example uses port 11000 on the local computer. IPHostEntry ipHostInfo = Dns. resolve (Dns. getHostName (); IPAddress ipAddress = ipHostInfo. addressList [0]; IPEndPoint remoteEP = new IPEndPoint (ipAddress, 1234); // Create a TCP/IP socket. socket sender = new Socket (AddressFamily. interNetwork, SocketType. stream, ProtocolType. tcp); // Connect the socket to the remote endpoint. catch any errors. try {sender. connect (remoteEP); // Console. writeLine ("Socket connected to {0}", sender. remoteEndPoint. toString (); txt. text + = "\ r \ n" + sender. remoteEndPoint. toString () + "\ r \ n"; // Encode the data string into a byte array. byte [] msg = Encoding. ASCII. getBytes (send_txt.Text +"
 
  
"); // Send the data through the socket. int bytesSent = sender. send (msg); // Receive the response from the remote device. int bytesRec = sender. receive (bytes); // Console. writeLine ("Echoed test = {0}", Encoding. ASCII. getString (bytes, 0, bytesRec); txt. text + = "\ r \ n" + Encoding. ASCII. getString (bytes, 0, bytesRec) + "\ r \ n"; // receives Remote Server Information // Release the socket. sender. shutdown (SocketShutdown. both); sender. close ();} catch (ArgumentNullException ane) {// Console. writeLine ("ArgumentNullException: {0}", ane. toString (); txt. text + = "\ r \ n" + ane. toString () + "\ r \ n";} catch (SocketException se) {// Console. writeLine ("SocketException: {0}", se. toString (); txt. text + = "\ r \ n" + se. toString () + "\ r \ n";} catch (Exception e) {// Console. writeLine ("Unexpected exception: {0}", e. toString (); txt. text + = "\ r \ n" + e. toString () + "\ r \ n" ;}} catch (Exception e) {// Console. writeLine (e. toString (); txt. text + = "\ r \ n" + e. toString () + "\ r \ n" ;}} public class SocketServer {int port; // port string host; // ip address TextBox txt ;///
  /// Number of constructor Han //////
  Port NumberPublic SocketServer (int ports, string host, TextBox txt) {this. port = ports; this. host = host; this.txt = txt;} // start listening to public void beginListen () {try {IPAddress ip = IPAddress. parse (host); // convert the ip address string to IPAddress type instance IPEndPoint ipe = new IPEndPoint (ip, port ); // use the specified port and ip address to initialize a new instance of the IPEndPoint class. // create a socket and start listening for Socket s = new Socket (AddressFamily. interNetwork, SocketType. stream, ProtocolType. tcp); // create a socket image. If udp is used, use SocketType. dgram-type socket s. bind (ipe); // Bind the EndPoint to the image (port and IP address) s. listen (0); // start listening for txt. text + = "waiting for client connection"; // Console. writeLine ("waiting for client connection"); // defines the loop so that the resume can be connected multiple times while (true) {Socket temp = s. accept (); // create a new socket while (true) {string recvStr = ""; byte [] recvBytes = new byte [1024]; int bytes; bytes = temp. receive (recvBytes, recvBytes. length, 0); // receive information from the client recvStr + = Encoding. ASCII. getString (recvBytes, 0, bytes); txt. text + = "\ r \ n" + recvStr + "\ r \ n"; if (recvStr. indexOf ("
  
   
")>-1) {break ;}// return information to the client // Console. writeLine ("server get message: {0}", recvStr); // display the client information string sendStr = "jpeg upload OK"; byte [] bs = Encoding. ASCII. getBytes (sendStr); temp. send (bs, bs. length, 0); // return the information to the client temp. shutdown (SocketShutdown. both); temp. close () ;}} catch (Exception ex) {string str = ex. toString (); txt. text + = "\ r \ n" + str + "\ r \ n ";}}}}
  
 

Client button event

   protected void Button1_Click(object sender, EventArgs e)        {                Socketclient client = new Socketclient(TextBox2, TextBox3);                client.StartClient();                   }


// The server listens to the execution thread

Protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {SocketServer socket = new SocketServer (1234, "192.168.88.196", TextBox1); // start Thread thread Thread = new Thread (new ThreadStart (socket. beginListen); thread. start (); // the code that runs when the application is started // (new System. threading. thread (new System. threading. threadStart (new Class1 (). creatSocket ))). start (); // open up a new thread }}


Aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JpegForSocketServer.aspx.cs" Inherits="WebForAndroid.JpegForSocketServer" %>
         


Figure:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.