c - 有没有办法在 c 中使用套接字编程实现两种 TCP 通信?
问题描述
我有两个代码文件,我正在尝试建立双向 TCP 通信。我有一种通信方式准备好并且可以正常工作,即将消息从客户端发送到服务器,然后服务器打印它。我一直在尝试建立双向 TCP 连接,这意味着我可以从任何一台机器发送消息。
客户端.c
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#define SERVER_PORT 5432
#define MAX_LINE 256
int main(int argc, char * argv[])
{
struct hostent *hp;
struct sockaddr_in sin;
char *host;
char buf[MAX_LINE];
int s;
int len, new_s;
if (argc==2) {
host = argv[1];
}
else {
fprintf(stderr, "usage: simplex-talk host\n");
exit(1);
}
/* translate host name into peer’s IP address */
hp = gethostbyname(host);
if (!hp) {
fprintf(stderr, "simplex-talk: unknown host: %s\n", host);
exit(1);
}
/* build address data structure */
bzero((char *)&sin, sizeof(sin));
sin.sin_family = AF_INET;
bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
sin.sin_port = htons(SERVER_PORT);
/* active open */
if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("simplex-talk: socket");
exit(1);
}
if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
perror("simplex-talk: connect");
close(s);
exit(1);
}
/* main loop: get and send lines of text */
/*while (fgets(buf, sizeof(buf), stdin)) {
buf[MAX_LINE-1] = '\0';
len = strlen(buf) + 1;
send(s, buf, len, 0);
}*/
while(1){
printf("sent: ");
if(fgets(buf, sizeof(buf), stdin)){
buf[MAX_LINE-1] = '\0';
len = strlen(buf)+1;
send(s, buf, len, 0);
}
if((len = recv(new_s, buf, sizeof(buf), 0)) != 0){
printf("received: ");
fputs(buf, stdout);
}
}
}
服务器.c
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#define SERVER_PORT 5432
#define MAX_PENDING 5
#define MAX_LINE 256
int main()
{
struct sockaddr_in sin;
char buf[MAX_LINE];
int len;
int s, new_s;
/* build address data structure */
bzero((char *)&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(SERVER_PORT);
/* setup passive open */
if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("simplex-talk: socket");
exit(1);
}
if ((bind(s, (struct sockaddr *)&sin, sizeof(sin))) < 0) {
perror("simplex-talk: bind");
exit(1);
}
listen(s, MAX_PENDING);
/* wait for connection, then receive and print text */
while(1) {
len = sizeof(sin);
if ((new_s = accept(s, (struct sockaddr *)&sin, (socklen_t *)&len)) < 0) {
perror("simplex-talk: accept");
exit(1);
}
while(1){
if((len = recv(new_s, buf, sizeof(buf), 0)) != 0){
printf("received: ");
fputs(buf, stdout);
}
printf("sent: ");
if(fgets(buf, sizeof(buf), stdin)){
buf[MAX_LINE-1] = '\0';
len = strlen(buf)+1;
send(s, buf, len, 0);
}
}
close(new_s);
}
}
汇编:
gcc -o server server.c
gcc -o client client.c
程序执行: 客户端:./client (ip address of the server machine)
服务器端:./server
预期结果:
客户端:
sent: hi
received: hello world
sent: what is your name?
服务器端:
received: hi
sent: hello world
received: what is your name?
实际结果:
客户端:
sent: hi
received: hi
sent: ^C
服务器端:
received: hi
sent: hello world
解决方案
推荐阅读
- javascript - 图片不是每次都刷新
- android - 警告 CS0618“Resource.Style.ThemeHoloDialog”已过时:“已弃用”
- android - API 令牌应该存储在客户端应用程序的什么位置?
- python - Python 3 pyplot.hold 已弃用
- mysql - 用于向表中添加新行的存储过程,它检查值以验证它们在外部表上的值
- php - 仅使用套接字从 PHP 直接向 Node 发送消息(不使用 Redis 等)
- algorithm - 将无向图转换为具有特定条件的有向图
- matlab - 我无法使用位于私人文件夹中的 MEX 功能
- dart - Dart Future 中的内部异常没有被捕获
- haskell - 使用 Bound 与多个类型变量进行抽象