Joke Collection Website - Blessing messages - Send SMS through serial port

Send SMS through serial port

Try using the socket;

Change the port number and ip address yourself.

Use System.Net;

Use the system. Net . Sockets

Static void Main(string[] args)// server segment

{

int port = 2000

string host = " 127 . 0 . 0 . 1 ";

/**/

///Create an endpoint

Ip address ip = address. Resolve (host); //Convert the ip address string to an instance of type IPaddress.

IPEndPoint ipe = new IPEndPoint(ip,port); //Initializes a new instance of the ip endpoint class with the specified port and IP.

/**/

///Create a socket and start listening.

Socket s = new sockets (AddressFamily. Internet, SocketType. Stream, protocol type. TCP); //Create a socket object. If udp protocol is used, please use a socket of type SocketType.Dgram.

South bind (IPE); //Bind endpoint objects (2000 ports and ip addresses)

Listen to the south (0); //Start listening

Console. WriteLine ("waiting for the client to connect");

/**/

///Accept the client connection, establish a new socket for this connection, and accept the information.

Socket temperature = s.accept (); //Create a new socket for the new connection.

Console. WriteLine ("establish connection");

string recvStr =

Byte[] recvBytes = new byte [1024];

Int byte;

Bytes = temperature. Receive (receive bytes, receive bytes. Length, 0); //receive information from the client

RecvStr += encoding. ASCII.GetString(recvBytes,0,bytes);

/**/

///Return information to the client.

Console. WriteLine ("server gets message: {0}", recvstr); //Displays information from the client.

string sendStr = "ok! The client sent the message successfully! ”;

Byte[] bs = encoding. ASCII . GetBytes(sendStr);

Send at the time of (bs, bs. Length, 0); //Return information to the client.

Close () when ...;

South close ();

Console. ReadLine();

}

Static void Main(string[] args)// client

{

attempt

{

int port = 2000

string host = " 127 . 0 . 0 . 1 ";

/**/

///Create an endpoint

Ip address ip = address. Resolve (host);

//IP address IPP = new IP address(" 127 . 0 . 0 . 1 ");

IPEndPoint ipe = new IPEndPoint(ip,port); //Convert ip and port to an instance of IPEndpoint.

/**/

///Create a socket and connect to the server.

Socket c = new socket (AddressFamily. Internet, SocketType. Stream, protocol type. TCP); //Create a socket

Console. WriteLine ("connect …");

C. connection (IPE); //Connect to the server

/**/

///Send information to the server

string sendStr = "hello! This is a socket test ";

Byte[] bs = encoding. ASCII . GetBytes(sendStr); //Encode the string into bytes

Console. WriteLine ("send message");

C. send (bs, bs. Length, 0); //Send information

/**/

///Accept the information returned by the server.

string recvStr =

Byte[] recvBytes = new byte [1024];

Int byte;

bytes = c.Receive(recvBytes,recvBytes。 Length, 0); //Receive the information returned by the server.

RecvStr += encoding. ASCII.GetString(recvBytes,0,bytes);

Console. WriteLine ("client gets message: {0}", recvstr); //Displays the information returned by the server.

/**/

///Be sure to close the socket after use.

c.close();

}

catch (ArgumentNullException e)

{

Console. WriteLine(" argumentNullException:{ 0 } ",e);

}

catch (SocketException e)

{

Console. WriteLine("SocketException:{0} ",e);

}

Console. WriteLine ("press enter to exit");

}