socket可用於客戶端和服務器端的通信
參考:https://blog.csdn.net/hellokitty136/article/details/81367996
ubuntu16.04上的程序:
服務器端:
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
//#include<Windows.h>
#include<netinet/in.h>
#include<sys/wait.h>
#include<unistd.h>
#include<arpa/inet.h>
//#include<winsock2.h>
//#pragma comment(lib, "ws2_32.lib")
#define PORT 1500//端口號
#define BACKLOG 5/*最大監聽數*/
int main(){
int sockfd, new_fd;/*socket句柄和建立連接後的句柄*/
struct sockaddr_in my_addr;/*本方地址信息結構體,下面有具體的屬性賦值*/
struct sockaddr_in their_addr;/*對方地址信息*/
//int sin_size;
socklen_t sin_size;
sockfd = socket(AF_INET, SOCK_STREAM, 0);//建立socket
if (sockfd == -1){
printf("socket failed:%d", errno);
return -1;
}
my_addr.sin_family = AF_INET;/*該屬性表示接收本機或其他機器傳輸*/
my_addr.sin_port = htons(PORT);/*端口號*/
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);/*IP,括號內容表示本機IP*/
bzero(&(my_addr.sin_zero), 8);/*將其他屬性置0*/
//memset(&(my_addr.sin_zero), 0, 8);
if (bind(sockfd, (struct sockaddr*)&my_addr, sizeof(struct sockaddr))<0){//綁定地址結構體和socket
printf("bind error");
return -1;
}
//system("pause");
listen(sockfd, BACKLOG);//開啓監聽 ,第二個參數是最大監聽數
while (1){
sin_size = sizeof(struct sockaddr_in);
new_fd = accept(sockfd, (struct sockaddr*)&their_addr, &sin_size);//在這裏阻塞知道接收到消息,參數分別是socket句柄,接收到的地址信息以及大小
if (new_fd == -1){
printf("receive failed");
}
else{
printf("receive success");
send(new_fd, "Hello World!", 12, 0);//發送內容,參數分別是連接句柄,內容,大小,其他信息(設爲0即可)
}
}
return 0;
}
客戶端:
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
//#include<Windows.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<sys/wait.h>
#include<arpa/inet.h>
#include<unistd.h>
//#include<winsock2.h>
//#pragma comment(lib, "ws2_32.lib")
#define DEST_PORT 1500//目標地址端口號
#define DEST_IP "127.0.0.1"/*目標地址IP,這裏設爲本機*/
#define MAX_DATA 100//接收到的數據最大程度
int main(){
int sockfd, new_fd;/*cocket句柄和接受到連接後的句柄 */
struct sockaddr_in dest_addr;/*目標地址信息*/
char buf[MAX_DATA];//儲存接收數據
sockfd = socket(AF_INET, SOCK_STREAM, 0);/*建立socket*/
if (sockfd == -1){
printf("socket failed:%d", errno);
}
//參數意義見上面服務器端
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(DEST_PORT);
dest_addr.sin_addr.s_addr = inet_addr(DEST_IP);
bzero(&(dest_addr.sin_zero), 8);
//memset(&(dest_addr.sin_zero), 0, 8);
//system("pause");
if (connect(sockfd, (struct sockaddr*)&dest_addr, sizeof(struct sockaddr)) == -1){//連接方法,傳入句柄,目標地址和大小
printf("connect failed:%d", errno);//失敗時可以打印errno
}
else{
printf("connect success\n");
recv(sockfd, buf, MAX_DATA, 0);//將接收數據打入buf,參數分別是句柄,儲存處,最大長度,其他信息(設爲0即可)。
printf("Received:%s\n", buf);
}
close(sockfd);
//closesocket(sockfd);//關閉socket
return 0;
}
用gcc編譯後生成兩個可執行程序 client 和service
在ubuntu上打開兩個終端,先執行./service,後執行./client ,輸出:
表示連接成功。
出現的錯誤有:
1.‘close’ was not declared in this scope
加上頭文件#include <unistd.h>
2.inet_addr was not declared in this scope
加上頭文件#include <arpa/inet.h>
理解socket 參考:
https://www.cnblogs.com/qtiger/p/5856660.html
如果在windows上執行可能遇到以下問題,都有相應的解決方案:
https://blog.csdn.net/junruitian/article/details/80999355