什么是socket,C中的socket怎么用?

什么是socket,C中的socket怎么用?,第1张

Socket 就是用来实现数据传输的一种方法

给你一段C中用Socket传输文件的代码,你参考怎么用

服务端代码:

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <sys/socket.h>

#include <sys/wait.h>

#define MYPORT 3490 /*定义用户连接端口*/

#define BACKLOG 10 /*多少等待连接控制*/

main()

{

int sockfd, new_fd/* listen on sock_fd, new connection on new_fd

*/

struct sockaddr_in my_addr/* my address information */

struct sockaddr_in their_addr/* connector's address information */

int sin_size

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {

perror("socket")

exit(1)

}

my_addr.sin_family = AF_INET/* host byte order */

my_addr.sin_port = htons(MYPORT)/* short, network byte order */

my_addr.sin_addr.s_addr = INADDR_ANY/* auto-fill with my IP */

bzero(&(my_addr.sin_zero),/* zero the rest of the struct */

if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct

sockaddr))== -1) {

perror("bind")

exit(1)

}

if (listen(sockfd, BACKLOG) == -1) {

perror("listen")

exit(1)

}

while(1) { /* main accept() loop */

sin_size = sizeof(struct sockaddr_in)

if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, \

&sin_size)) == -1) {

perror("accept")

continue

}

printf("server: got connection from %s\n", \

inet_ntoa(their_addr.sin_addr))

if (!fork()) { /* this is the child process */

if (send(new_fd, "Hello, world!\n", 14, 0) == -1)

perror("send")

close(new_fd)

exit(0)

}

close(new_fd)/* parent doesn't need this */

while(waitpid(-1,NULL,WNOHANG) >0)/* clean up child processes */

}

}

客户代码:

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <sys/socket.h>

#include <sys/wait.h>

#define PORT 3490 /* 客户机连接远程主机的端口 */

#define MAXDATASIZE 100 /* 每次可以接收的最大字节 */

int main(int argc, char *argv[])

{

int sockfd, numbytes

char buf[MAXDATASIZE]

struct hostent *he

struct sockaddr_in their_addr/* connector's address information */

if (argc != 2) {

fprintf(stderr,"usage: client hostname\n")

exit(1)

}

if ((he=gethostbyname(argv[1])) == NULL) { /* get the host info */

herror("gethostbyname")

exit(1)

}

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {

perror("socket")

exit(1)

}

their_addr.sin_family = AF_INET/* host byte order */

their_addr.sin_port = htons(PORT)/* short, network byte order */

their_addr.sin_addr = *((struct in_addr *)he->h_addr)

bzero(&(their_addr.sin_zero),/* zero the rest of the struct */

if (connect(sockfd, (struct sockaddr *)&their_addr,sizeof(struct

sockaddr)) == -1) {

perror("connect")

exit(1)

}

if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1) {

perror("recv")

exit(1)

}

buf[numbytes] = '\0'

printf("Received: %s",buf)

close(sockfd)

return 0

}

(1)编辑include/linux/socket.h,定义新的协议域和相关结构:

定义新的协议域:

#define PF_NEW_DOMAIN 17

定义新的套接字结构:

struct sockaddr_new_domain {

short sn_family/* 套接字类型 */

char sn_data[128]/* 地址数据 */

}

(2)编辑net/socket.c,定义新的协议函数

static struct net_proto_family new_domain_family_ops = {

.family = PF_NEW_DOMAIN,

.create = new_domain_create,

.owner = THIS_MODULE,

}

/* 新协议创建函数 */

int new_domain_create(struct net *net, struct socket *sock,

int protocol, int kern)

{

/* 创建新的套接字 */

return 0

}

(3)编辑net/core/sock.c,把新的协议函数注册到内核:

/* 注册新协议函数 */

void __init new_domain_init(void)

{

sock_register(&new_domain_family_ops)

}

(4)编辑net/core/dev.c,实现新的协议处理函数:

/* 新的协议处理函数 */

int new_domain_handler(struct sk_buff *skb)

{

/* 处理新的协议数据包 */

return 0

}

(5)编辑net/core/net_namespace.c,把新的协议处理函数注册到内核:

/* 注册新的协议处理函数 */

void __init new_domain_init(void)

{

dev_add_pack(&new_domain_packet_type)

}

#include "stm32f10x_conf.h"这个文件里已经把所有的给包含进去了,

#include "stm32f10x_adc.h"

//#include "stm32f10x_bkp.h"

//#include "stm32f10x_can.h"

//#include "stm32f10x_cec.h"

//#include "stm32f10x_crc.h"

//#include "stm32f10x_dac.h"

//#include "stm32f10x_dbgmcu.h"

#include "stm32f10x_dma.h"

//#include "stm32f10x_exti.h"

//#include "stm32f10x_flash.h"

#include "stm32f10x_fsmc.h"

#include "stm32f10x_gpio.h"

//#include "stm32f10x_i2c.h"

//#include "stm32f10x_iwdg.h"

//#include "stm32f10x_pwr.h"

#include "stm32f10x_rcc.h"

//#include "stm32f10x_rtc.h"

#include "stm32f10x_sdio.h"

//#include "stm32f10x_spi.h"

#include "stm32f10x_tim.h"

#include "stm32f10x_usart.h"

//#include "stm32f10x_wwdg.h"、

你需要把那些不需要的给注释掉。。。。不行的话欢迎反馈交流


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11859885.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存