为什么 linux服务器 默认是tcp6 的端口
答案:2 悬赏:80 手机版
解决时间 2021-12-25 20:31
- 提问者网友:藍了天白赴美
- 2021-12-25 02:55
为什么 linux服务器 默认是tcp6 的端口
最佳答案
- 五星知识达人网友:深街酒徒
- 2021-12-25 03:44
客户端代码:
#include
#include
#include
#include
#include
#include
#define SA struct sockaddr
#define MAXLINE 1024
int
main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr;
if (argc != 2) printf("usage: udpcli");
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(8889);
//Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
dg_cli(stdin, sockfd, (SA *) &servaddr, sizeof(servaddr));
exit(0);
}
int
dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)
{
int n;
char sendline[MAXLINE], recvline[MAXLINE + 1];
while (fgets(sendline, MAXLINE, fp) != NULL) {
printf("the client received the message is:\n");
sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);
n = recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL);
recvline[n] = '\0';
fputs(recvline, stdout);
}
}
服务器代码:
#include
#include
#include
#include
#include
#include
#define SA struct sockaddr
#define MAXLINE 1023
int main(int argc, char** argv)
{ int sockfd;
struct sockaddr_in servaddr, cliaddr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(8889);
bind(sockfd, (SA *) &servaddr, sizeof(servaddr));
dg_echo(sockfd, (SA *) &cliaddr, sizeof(cliaddr));
return 0;
}
int
dg_echo(int sockfd, SA *pcliaddr, socklen_t clilen)
{
int n;
socklen_t len;
char mesg[MAXLINE];
for ( ; ; ) {
len = clilen;
printf("the server received the message is:\n");
while((n = recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len))>0) {
mesg[n] ='\0';
fputs(mesg, stdout);
sendto(sockfd, mesg, n, 0, pcliaddr, len);
}
}
}
#include
#include
#include
#include
#include
#include
#define SA struct sockaddr
#define MAXLINE 1024
int
main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr;
if (argc != 2) printf("usage: udpcli
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(8889);
//Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
dg_cli(stdin, sockfd, (SA *) &servaddr, sizeof(servaddr));
exit(0);
}
int
dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)
{
int n;
char sendline[MAXLINE], recvline[MAXLINE + 1];
while (fgets(sendline, MAXLINE, fp) != NULL) {
printf("the client received the message is:\n");
sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);
n = recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL);
recvline[n] = '\0';
fputs(recvline, stdout);
}
}
服务器代码:
#include
#include
#include
#include
#include
#include
#define SA struct sockaddr
#define MAXLINE 1023
int main(int argc, char** argv)
{ int sockfd;
struct sockaddr_in servaddr, cliaddr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(8889);
bind(sockfd, (SA *) &servaddr, sizeof(servaddr));
dg_echo(sockfd, (SA *) &cliaddr, sizeof(cliaddr));
return 0;
}
int
dg_echo(int sockfd, SA *pcliaddr, socklen_t clilen)
{
int n;
socklen_t len;
char mesg[MAXLINE];
for ( ; ; ) {
len = clilen;
printf("the server received the message is:\n");
while((n = recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len))>0) {
mesg[n] ='\0';
fputs(mesg, stdout);
sendto(sockfd, mesg, n, 0, pcliaddr, len);
}
}
}
全部回答
- 1楼网友:洒脱疯子
- 2021-12-25 04:02
在路由器和防火墙那里处理,开放端口,做端口转移。
你的服务器会通过路由获得一个外网地址呀。
你也可以利用路由器配置给你的服务器单独一个外网ip,这些内容和操作,联系网络管理员,或你们配置路由的那个人,就可以搞定。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯