···
#include <stdio.h>
#include <string.h>
#include <errno.h>衡枯帆
#include <stdlib.h>咐雹
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define DEST_PORT 6020
#define DSET_IP_ADDRESS "192.168.1.92"
int main()
{
/* socket锟侥硷拷锟斤拷锟斤拷锟斤拷 */
int sock_fd
/* udp socket */
sock_fd = socket(AF_INET, SOCK_DGRAM, 0)
if(sock_fd <0)
{
perror("socket")
exit(1)
}
/* 锟斤拷锟斤拷address */
struct sockaddr_in addr_serv
int len
memset(&addr_serv, 0, sizeof(addr_serv))
addr_serv.sin_family = AF_INET
addr_serv.sin_addr.s_addr = inet_addr(DSET_IP_ADDRESS)
addr_serv.sin_port = htons(DEST_PORT)
len = sizeof(addr_serv)
int send_num
int recv_num
char send_buf[100]={0}
send_buf[0]=0x23
send_buf[1]=0x23
send_buf[2]=0x23
send_buf[3]=0x23
败罩 send_buf[4]=0x4a
send_buf[5]=0
send_buf[6]=0
send_buf[7]=0
send_buf[8]=0x4a
send_buf[9]=0
send_buf[10]=0
send_buf[11]=0
send_buf[12]=0x01
send_buf[13]=0
send_buf[14]=0
send_buf[15]=0
send_buf[16]=0xFF
send_buf[17]=0xCC
send_buf[18]=0x0B
send_buf[19]=0x01
uint8_t mycheck
int i=0
// char recv_buf[20]
while(1){
send_buf[65]=0x01
for(i=0i<71i++)
{
mycheck ^= send_buf[i]
}
send_buf[71]= mycheck
send_buf[72]=0xEE
send_buf[73]=0xDD
//printf("client send: %s\n", send_buf)
sleep(1)
send_num = sendto(sock_fd, send_buf, 74, 0, (struct sockaddr *)&addr_serv, len)
if(send_num <0)
{
perror("sendto error:")
exit(1)
}
/* recv_num = recvfrom(sock_fd, recv_buf, sizeof(recv_buf), 0, (struct sockaddr *)&addr_serv, (socklen_t *)&len)
if(recv_num <0)
{
perror("recvfrom error:")
exit(1)
}
recv_buf[recv_num] = '\0'
printf("client receive %d bytes: %s\n", recv_num, recv_buf)
}
close(sock_fd)
return 0
*/
}}
···
服务器端
...
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#define SERV_PORT 6020
int main()
{
/* sock_fd --- socket文件描述符 创建udp套接字*/
int sock_fd = socket(AF_INET, SOCK_DGRAM, 0)
if(sock_fd <0)
{
perror("socket")
exit(1)
}
struct sockaddr_in addr_serv
int len
memset(&addr_serv, 0, sizeof(struct sockaddr_in)) //每个字节都用0填充
addr_serv.sin_family = AF_INET //使用IPV4地址
addr_serv.sin_port = htons(SERV_PORT) //端口
/* INADDR_ANY表示不管是哪个网卡接收到数据,只要目的端口是SERV_PORT,就会被该应用程序接收到 */
addr_serv.sin_addr.s_addr = htonl(INADDR_ANY) //自动获取IP地址
len = sizeof(addr_serv)
/* 绑定socket */
if(bind(sock_fd, (struct sockaddr *)&addr_serv, sizeof(addr_serv)) <0)
{
perror("bind error:")
exit(1)
}
int recv_num
int send_num
char send_buf[20] = "i am server!"
char recv_buf[100]
struct sockaddr_in addr_client
while(1)
{
printf("server wait:\n")
recv_num = recvfrom(sock_fd, recv_buf, sizeof(recv_buf), 0, (struct sockaddr *)&addr_client, (socklen_t *)&len)
if(recv_num <0)
{
perror("recvfrom error:")
exit(1)
}
recv_buf[recv_num] = '\0'
printf("server receive %d bytes: %s\n", recv_num, recv_buf)
send_num = sendto(sock_fd, send_buf, recv_num, 0, (struct sockaddr *)&addr_client, len)
if(send_num <0)
{
perror("sendto error:")
exit(1)
}
}
close(sock_fd)
return 0
}
...
执行客户端、服务端命令即可:
简单的学习代码,仅供参考:
server.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define SERVICE_PORT 8080
#define BUFSIZE 2048
#define HEAD 4
int main(int argc, char **argv)
{
struct sockaddr_in myaddr /* our address */
struct 让拦sockaddr_in remaddr /* remote address */
socklen_t addrlen = sizeof(remaddr) /* length of addresses */
int recvlen /* # bytes received */
int fd /* our socket */
int msgcnt = 0 /* count # of messages we received */
unsigned char buf[BUFSIZE]="ret_" /* receive buffer */
/* create a UDP socket */
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("cannot create socket\n")
return 0
}
/* bind the socket to any valid IP address and a specific port */
memset((char *)&myaddr, 0, sizeof(myaddr))
myaddr.sin_family = 芹隐AF_INET
myaddr.sin_addr.s_addr = htonl(INADDR_ANY)
myaddr.sin_port = htons(SERVICE_PORT)
if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
perror("bind failed")
return 0
}
/* now loop, receiving data and printing what we received */
for ()
{
printf("waiting on port %d\n", SERVICE_PORT)
recvlen = recvfrom(fd, buf+HEAD, BUFSIZE, 0, (struct sockaddr *)&remaddr, &addrlen)
if (recvlen > 0)
{
buf[HEAD+recvlen] = 0
printf("received message: \"%s\" (%d bytes)\n", buf+HEAD, recvlen)
}
else
{
printf("uh oh - something went wrong!\n")
}
printf("sending response \"%s\"\n", buf)
if (sendto(fd, buf, strlen(buf), 0, (struct sockaddr *)&remaddr, addrlen) < 0)
{
perror("sendto")
}
}
close(fd)
return 0
}
client.c
#include <stdlib.h>#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <sys/socket.h>
#define SERVICE_PORT 8080
#define BUFLEN 2048
int main( int argc,char* argv[] )
{
struct 嫌滑厅sockaddr_in myaddr, remaddr
int fd, i, slen=sizeof(remaddr)
char sendbuf[BUFLEN]="hello" /* message buffer */
char readbuf[BUFLEN]={0}
int recvlen /* # bytes in acknowledgement message */
char *server = "127.0.0.1" /* change this to use a different server */
/* create a socket */
if ((fd=socket(AF_INET, SOCK_DGRAM, 0))==-1)
{
printf("socket created\n")
}
/* bind it to all local addresses and pick any port number */
memset((char *)&myaddr, 0, sizeof(myaddr))
myaddr.sin_family = AF_INET
myaddr.sin_addr.s_addr = htonl(INADDR_ANY)
myaddr.sin_port = htons(0)
if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0)
{
perror("bind failed")
return 0
}
memset((char *) &remaddr, 0, sizeof(remaddr))
remaddr.sin_family = AF_INET
remaddr.sin_port = htons(SERVICE_PORT)
if (inet_aton(server, &remaddr.sin_addr)==0)
{
fprintf(stderr, "inet_aton() failed\n")
exit(1)
}
printf("Sending packet %d to %s port %d\n", i, server, SERVICE_PORT)
if (sendto(fd, sendbuf, strlen(sendbuf), 0, (struct sockaddr *)&remaddr, slen)==-1)
{
perror("sendto")
exit(1)
}
/* now receive an acknowledgement from the server */
recvlen = recvfrom(fd, readbuf, BUFLEN, 0, (struct sockaddr *)&remaddr, &slen)
if ( recvlen >= 0 )
{
readbuf[recvlen] = 0 /* expect a printable string - terminate it */
printf("received message: \"%s\"\n", readbuf)
}
close(fd)
return 0
}
如果需要整个工程,可以联系我。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)