socket也就不介紹了,相關的都不少,能夠在在網上搜。環境是本機爲win10,再虛擬一臺Ubantu做爲linux環境。下面是DEMO。linux
注意 : windos 與 Linux系統有編碼差別,windos爲GBK, Linux爲utf-8,通訊時按各自須要轉換編碼,這裏採用通用UTF-8格式。服務器
一、Linux_C服務端dom
1 #include <sys/types.h> 2 #include <sys/socket.h> 3 #include <stdio.h> 4 #include <netinet/in.h> 5 #include <arpa/inet.h> 6 #include <unistd.h> 7 #include <string.h> 8 #include <stdlib.h> 9 #include <fcntl.h> 10 #include <sys/shm.h> 11 12 #define MYPORT 8887 13 #define QUEUE 20 14 #define BUFFER_SIZE 1024 15 #define RETURN_BUFFER_SIZE 102400 16 17 int main() 18 { 19 ///定義sockfd 20 /* 21 --------domain用於指定建立套接字所使用的協議族 22 AF_UNIX # 建立只在本機進行通訊的套接字; 23 AF_INET # 使用IPv4TCP/IP協議; 24 AF_INET6 # 使用IPv6TCP/IP協議 25 ---------type用於指定套接字的類型。 26 SOCK_STREAM # 建立TCP流套接字; 27 SOCK_DGRAM # 建立DUP套接字; 28 SOCK_RAW # 建立原始套接字; 29 ----------protocol一般設定爲0,表示經過參數domain指定的協議族和參數type指定的套接字類型來肯定使用的協議。 30 */ 31 int server_sockfd = socket(AF_INET,SOCK_STREAM, 0); 32 33 ///定義sockaddr_in 34 struct sockaddr_in server_sockaddr; 35 server_sockaddr.sin_family = AF_INET; 36 server_sockaddr.sin_port = htons(MYPORT); 37 server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY); 38 39 ///bind,成功返回0,出錯返回-1 40 if(bind(server_sockfd,(struct sockaddr *)&server_sockaddr,sizeof(server_sockaddr))==-1) 41 { 42 perror("bind"); 43 exit(1); 44 } 45 46 ///listen,成功返回0,出錯返回-1 47 if(listen(server_sockfd,QUEUE) == -1) 48 { 49 perror("listen"); 50 exit(1); 51 } 52 53 ///客戶端套接字 54 char buffer[BUFFER_SIZE]; 55 char returnBuffer[RETURN_BUFFER_SIZE]; 56 struct sockaddr_in client_addr; 57 socklen_t length = sizeof(client_addr); 58 59 printf("socket server is started! \n"); 60 ///成功返回非負描述字,出錯返回-1 61 int conn = accept(server_sockfd, (struct sockaddr*)&client_addr, &length); 62 if(conn<0) 63 { 64 perror("connect"); 65 exit(1); 66 } 67 68 printf("a Client connected : %u \n",client_addr.sin_addr.s_addr); 69 70 while(1) 71 { 72 int exitMark = 0; 73 memset(buffer,0,sizeof(buffer)); 74 int len = recv(conn, buffer, sizeof(buffer),0); 75 76 77 if(strcmp(buffer,"exit\n")==0) 78 { 79 exitMark = 1; 80 // attachMsg = "服務器被指定命令[exit]成功關閉!"; 81 // break; 82 } 83 else 84 { 85 // attachMsg = " this Msg by server send!"; 86 } 87 88 // strcpy(attachMsg+strlen(attachMsg),buffer); 89 printf("GetMsg: %s",buffer); 90 91 // fputs(buffer, stdout); 92 send(conn, buffer, len, 0); 93 94 if(exitMark) 95 { 96 printf("socket server is closed! \n"); 97 break; 98 } 99 } 100 101 102 close(conn); 103 close(server_sockfd); 104 105 return 0; 106 }
二、Linux_C客戶端socket
1 #include <sys/types.h> 2 #include <sys/socket.h> 3 #include <stdio.h> 4 #include <netinet/in.h> 5 #include <arpa/inet.h> 6 #include <unistd.h> 7 #include <string.h> 8 #include <stdlib.h> 9 #include <fcntl.h> 10 #include <sys/shm.h> 11 12 #define MYPORT 8887 13 #define BUFFER_SIZE 1024 14 15 int main() 16 { 17 ///定義sockfd 18 int sock_cli = socket(AF_INET,SOCK_STREAM, 0); 19 20 ///定義sockaddr_in 21 struct sockaddr_in servaddr; 22 memset(&servaddr, 0, sizeof(servaddr)); 23 servaddr.sin_family = AF_INET; 24 servaddr.sin_port = htons(MYPORT); ///服務器端口 25 servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); ///服務器ip 26 27 ///鏈接服務器,成功返回0,錯誤返回-1 28 if (connect(sock_cli, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) 29 { 30 perror("connect"); 31 exit(1); 32 } 33 34 char sendbuf[BUFFER_SIZE]; 35 char recvbuf[BUFFER_SIZE]; 36 while (fgets(sendbuf, sizeof(sendbuf), stdin) != NULL) 37 { 38 send(sock_cli, sendbuf, strlen(sendbuf),0); ///發送 39 if(strcmp(sendbuf,"exit\n")==0) 40 break; 41 recv(sock_cli, recvbuf, sizeof(recvbuf),0); ///接收 42 // fputs(recvbuf, stdout); 43 printf("Client GetMsg: %s",recvbuf); 44 memset(sendbuf, 0, sizeof(sendbuf)); 45 memset(recvbuf, 0, sizeof(recvbuf)); 46 } 47 48 close(sock_cli); 49 return 0; 50 }
三、Windos_C#客戶端ide
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Net.Sockets; 7 using System.IO; 8 using System.Net; 9 10 11 namespace socketClientForLinuxC 12 { 13 class Program 14 { 15 static void Main(string[] args) 16 { 17 string sendMsg = string.Empty; 18 string recvMsg = string.Empty; 19 int sendMark = 0; 20 21 IPAddress address = IPAddress.Parse("192.168.30.137"); 22 IPEndPoint iep = new IPEndPoint(address, 8887); 23 //建立一個Socket 24 Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 25 socket.Connect(iep); 26 27 while (true) 28 { 29 sendMsg = Console.ReadLine(); 30 if (sendMsg.Equals("exit")) 31 { 32 sendMark = 1; 33 } 34 35 byte[] bs = Encoding.UTF8.GetBytes(sendMsg); 36 socket.Send(bs, bs.Length, 0); 37 38 byte[] recvBytes = new byte[1024]; 39 int bytes; 40 41 //從服務器端接受返回信息 42 bytes = socket.Receive(recvBytes, recvBytes.Length, 0); 43 recvMsg = Encoding.UTF8.GetString(recvBytes, 0, bytes); 44 45 46 if (sendMark == 1) break; 47 Console.WriteLine(recvMsg); 48 } 49 50 socket.Close(); 51 52 Console.ReadKey(); 53 } 54 } 55 }