如何用C实现一个tcp的端口广播

如何用C实现一个tcp的端口广播,第1张

//windows TCP Client# include "winsock2.h"void main(void){WSADATA wsaDataSOCKET sSOCKADDR_IN ServerAddrint Port = 80 //初始化Windows Socket 2.2 WSAStartup(MAKEWORD(2,2),wsaData) // 创建一个新的Socket来连接服务器 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) // 填写客户端地址信息// 端口为80// 服务器IP地址为"192.168.1.1",注意穗纤使用inet_addr将IP地址猜闹仿转换为网络格式 ServerAddr.sin_family = AF_INETServerAddr.sin_port = htons(Port)ServerAddr.sin_addr.s_addr = inet_addr("192.168.1.1") // 向服务器发出连接请求 connect(s, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr)) // 新的连接建立后,就可以互相通信了,在这个简单的例子中,我们直接关闭连接,// 并关弯闷闭监听Socket,然后退出应用程序 closesocket(s) // 释放Windows Socket DLL的相关资源 WSACleanup()}

UDP的,你看下

1.服务器端实现

程序在袭樱收到客户端发送来的消息后,给客拍手丛户端发送消息,提示客户端收到薯如了该消息

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <netdb.h>

#include <stdio.h>

#include <unistd.h>

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

{

int sock, length, fromlen, n

struct sockaddr_in server

struct sockaddr_in from

char buf[1024]

//要求执行是输入端口信息

if (argc!= 2) {

printf( "Usage: %s port_num\n",argv[0])

return 1

}

//创建通信所需的套接字,并与地址和端口信息帮定

sock=socket(AF_INET, SOCK_DGRAM, 0)

if (sock <0){

perror("cannot create communicating socket")

return 1

}

length = sizeof(server)

bzero(&server,length)

server.sin_family=AF_INET

server.sin_addr.s_addr=INADDR_ANY

server.sin_port=htons(atoi(argv[1]))

if (bind(sock,(struct sockaddr *)&server,length)<0){

perror("cannot bind the socket")

close(sock)

return 1

}

fromlen = sizeof(struct sockaddr_in)

//读取客户端发送来的信息,显示后,发回相关信息给客户端

while (1) {

n = recvfrom(sock,buf,sizeof(buf),0,(struct sockaddr *)&from,&fromlen)

if (n <0) {

perror("cannot receive date from client")

break

}

write(STDOUT_FILENO,"server: Received a datagram: ",29)

write(STDOUT_FILENO,buf,n)

n = sendto(sock,"send message to client\n",22,

0,(struct sockaddr *)&from,fromlen)

if (n <0) {

perror("cannot send data to the client")

break

}

}

close(sock)

return 0

}

2.客户端实现

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <netdb.h>

#include <stdio.h>

#include <unistd.h>

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

{

int sock, length, n

struct sockaddr_in server, from

struct hostent *hp

char buffer[256]

//判断输入参数是否符合要求

if (argc != 3) {

printf("Usage: %s server_ip port_num\n",argv[0])

return 1

}

//创建通信套接字

sock= socket(AF_INET, SOCK_DGRAM, 0)

if (sock <0) {

perror("cannot create communicating socket")

return 1

}

server.sin_family = AF_INET

hp = gethostbyname(argv[1])

if (hp==0) {

perror("cannot get the server ip address")

return 1

}

bcopy((char *)hp->h_addr,

(char *)&server.sin_addr,

hp->h_length)

server.sin_port = htons(atoi(argv[2]))

length=sizeof(struct sockaddr_in)

printf("(client) enter the message: ")

bzero(buffer,256)

fgets(buffer,255,stdin)

//发送数据给指定服务器

n=sendto(sock,buffer,strlen(buffer),0,&server,length)

if (n <0){

perror("cannot get message from the client")

return 1

}

//从服务器中接受数据

bzero(buffer,256)

n = recvfrom(sock,buffer,256,0,&from, &length)

if (n <0) {

perror("cannot send message to the server")

return 1

}

printf("client got message : %s\n",buffer)

close(sock)

return 0

}

//发送端程序

#include <stdio.h>

#include <winsock.h>

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

{

WSADATA wsaData //指向WinSocket信息结构的指针

SOCKET sockListener

SOCKADDR_IN sin,saUdpServ

BOOL fBroadcast = TRUE

char sendBuff[1024]

int nSize

int ncount=0

if(WSAStartup(MAKEWORD( 1, 1 ), &wsaData )!=0)//进行WinSocket的初始李虚慎化

{

printf("Can't initiates windows socket!Program stop.\n")//初始化失败返回-1

return -1

}

sockListener=socket(PF_INET,SOCK_DGRAM,0)

setsockopt ( sockListener,SOL_SOCKET,SO_BROADCAST,

(CHAR *)&fBroadcast,sizeof ( BOOL ))

sin.sin_family = AF_INET

sin.sin_port = htons(0)

sin.sin_addr.s_addr = htonl(INADDR_ANY)

if(bind( sockListener, (SOCKADDR *)&sin, sizeof(sin))!=0)

{

printf("Can't bind socket to local port!Program stop.\n")//初始化失败返哪敬回-1

return -1

}

saUdpServ.sin_family = AF_INET

saUdpServ.sin_addr.s_addr = htonl ( INADDR_BROADCAST )

saUdpServ.sin_port = htons (7001)//发送用的端口,可以根据需要更改

nSize = sizeof ( SOCKADDR_IN )

while(1)

{

sprintf(sendBuff,"Message %d",ncount++)

sendto ( sockListener,sendBuff,

lstrlen (sendBuff),

0,

(SOCKADDR *) &saUdpServ,

sizeof ( SOCKADDR_IN ))

printf("%s\n",sendBuff)

}

return 0

}

/////////////////////

//接收

#include <stdio.h>

#include <winsock.h>

#include <conio.h>

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

{

WSADATA wsaData //指向WinSocket信息结构的指针

SOCKET sockListener

SOCKADDR_IN sin,saClient

char cRecvBuff[1024]

int nSize,nbSize

int iAddrLen=sizeof(saClient)

if(WSAStartup(MAKEWORD( 1, 1 ), &wsaData )!=0)//进行WinSocket的初始化

{

printf("Can't initiates windows socket!Program stop.\n")//初誉激始化失败返回-1

return -1

}

sockListener=socket(AF_INET, SOCK_DGRAM,0)

sin.sin_family = AF_INET

sin.sin_port = htons(7001)//发送端使用的发送端口,可以根据需要更改

sin.sin_addr.s_addr = htonl(INADDR_ANY)

if(bind( sockListener, (SOCKADDR FAR *)&sin, sizeof(sin))!=0)

{

printf("Can't bind socket to local port!Program stop.\n")//初始化失败返回-1

return -1

}

while(1)

{

nSize = sizeof ( SOCKADDR_IN )

if((nbSize=recvfrom (sockListener,cRecvBuff,1024,0,

(SOCKADDR FAR *) &saClient,&nSize))==SOCKET_ERROR)

{

printf("Recive Error")

break

}

cRecvBuff[nbSize] = '\0'

printf("%s\n",cRecvBuff)

}

return 0

}


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

原文地址: http://outofmemory.cn/yw/12408324.html

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

发表评论

登录后才能评论

评论列表(0条)

保存