iOS client:
Download the Asyncsocket Development Framework and drag it into the project
Establish
#import "ViewController.h"
#import <sys/socket.h>
#import <netinet/in.h>
#import <arpa/inet.h>
#import <unistd.h>
#import "AsyncSocket.h"
#define DEVW [[UIScreen mainscreen]bounds].size.width
@interface Viewcontroller ()
{
Asyncsocket *asysocket;
}
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
Asysocket = [[Asyncsocket alloc] initwithdelegate:self];
Nserror *err = nil;
if (! [Self socketopen:@ "172.16.1.92" port:1212])
{
NSLog (@ "Error:%@", err);
}
Additional setup after loading the view, typically from a nib.
}
-(void) SendData: (UIButton *) click
{
Nsmutablestring *sendstring=[nsmutablestring stringwithcapacity:1000];
[sendstring appendstring:@ "Hello world!"];
NSData *cmddata = [sendstring datausingencoding:nsutf8stringencoding];
[Asysocket writedata:cmddata withtimeout:-1 tag:0];
}
Open it
-(Nsinteger) Socketopen: (nsstring*) addr port: (Nsinteger) port
{
if (![ Asysocket isconnected])
{
[Asysocket connecttohost:addr onport:port withtimeout:-1 Error:nil];
NSLog (@ "Connect to host:%@ port:%d", addr,port);
}
return 0;
}
-(void) Onsocket: (Asyncsocket *) sock willdisconnectwitherror: (Nserror *) err
{
NSLog (@ "willdisconnectwitherror:%@", err);
}
-(void) Onsocketdiddisconnect: (Asyncsocket *) sock
{
NSLog (@ "Onsocketdiddisconnect");
}
-(void) Onsocket: (Asyncsocket *) sock didconnecttohost: (NSString *) host port: (UInt16) port
{
NSLog (@ "Didconnecttohost");
This is the asynchronous return of the connection success,
[sock readdatawithtimeout:-1 tag:0];
}
-(void) Onsocket: (Asyncsocket *) sock didreaddata: (NSData *) data withtag: (long) tag
{
NSString *msg = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding];
if (msg)
{
Processing the data received
NSLog (@ "Data received:%@", msg);
}
Else
{
NSLog (@ "Error converting received data into UTF-8 String");
}
NSString *[email protected] "connected successfully";
NSData *cmddata = [message datausingencoding:nsutf8stringencoding];
[Sock Writedata:cmddata Withtimeout:-1 tag:0];
[sock readdatawithtimeout:-1 tag:0];
}
-(void) Onsocket: (Asyncsocket *) sock Didwritedatawithtag: (long) tag
{
NSLog (@ "didwritedatawithtag:%ld", tag);
[sock readdatawithtimeout:-1 tag:0];
}
C # Service side:
Server-side using system;using system.collections.generic;using system.componentmodel;using system.data;using System.drawing;using system.linq;using system.text;using system.windows.forms;using System.Net.Sockets;using system.net;using system.threading;namespace server{public partial class Form1:form {Socket socketsend; Public Form1 () {InitializeComponent (); Checkforillegalcrossthreadcalls = false; }//Listen private void Button1_Click (object sender, EventArgs e) {Socket socketwatch = new S Ocket (ADDRESSFAMILY.INTERNETWORK,SOCKETTYPE.STREAM,PROTOCOLTYPE.TCP); IPAddress IP = ipaddress.any; IPEndPoint point = new IPEndPoint (IP, Convert.ToInt32 (this.txtPort.Text)); Socketwatch.bind (point); ShowMsg ("monitor success"); Monitor Socketwatch.listen (10); Use threads to constantly listen for thread thread = new Thread (Listen); Thread. IsBackground = true; Thread. Start (Socketwatch); }//Send message private void button2_click (object sender, EventArgs e) {string msg = This.txtmsg . Text.trim (); byte[] buffer = Encoding.UTF8.GetBytes (msg); Socketsend.send (buffer); }//Listener client socket void Listen (object o) {socket socketwatch = O as socket; while (true) {//waits for the client to connect and creates a socket for communication socketsend = Socketwatch.accept () ; ShowMsg (socketSend.RemoteEndPoint.ToString () + ":" + "connection successful"); Accept the message sent by the client thread thread = new thread (receive); Thread. IsBackground = true; Thread. Start (Socketsend); }} void Receive (object o) {socket socketsend = O as socket; while (true) {byte[] buffer = new BYTE[1024 * 1024 * 2]; Int r = socketsend.receive (buffer); if (r = = 0) {break; The string msg = Encoding.UTF8.GetString (buffer,0,r); ShowMsg (socketSend.RemoteEndPoint.ToString () + ":" + msg); }} void ShowMsg (String msg) {txtlog.appendtext (msg+ "\ r \ n"); } }}
WinForm Client
Client using system;using system.collections.generic;using system.componentmodel;using system.data;using System.drawing;using system.linq;using system.text;using system.windows.forms;using System.Net.Sockets;using system.net;using system.threading;namespace client{public partial class Form1:form {public Form1 () {InitializeComponent (); Checkforillegalcrossthreadcalls = false; } Socket Socketsend; private void Button1_Click (object sender, EventArgs e) {socketsend = new Socket (ADDRESSFAMILY.INTERNETW ORK,SOCKETTYPE.STREAM,PROTOCOLTYPE.TCP); Get the server IP IPAddress IP = ipaddress.parse (TxtIp.Text.Trim ()); Gets the port number IPEndPoint point = new IPEndPoint (IP, Convert.ToInt32 (TxtPort.Text.Trim ())); Connection Server Socketsend.connect (point); Open a thread that continuously accepts messages sent from the server thread thread = new thread (receive); Thread. IsBackground = true; Thread. Start (); }///<summary>///Receive information sent from the server///</summary> void receive () { while (true) {byte[] buffer = new BYTE[1024 * 1024 * 2]; int r = socketsend.receive (buffer); if (r = = 0) {break; } string msg = Encoding.UTF8.GetString (buffer, 0, R); ShowMsg (Socketsend.remoteendpoint + ":" + msg); }}////<summary>//customer-side Send information to server///</summary>//<param name= "Sende R "></param>//<param name=" E "></param> private void button2_click (object sender, even Targs e) {string msg = This.txtMsg.Text.Trim (); byte[] buffer = Encoding.UTF8.GetBytes (msg); Socketsend.send (buffer); } void ShowMsg (String msg) { Txtlog.appendtext (msg + "\ r \ n"); } }}
IOS Socket Notes