我刚开始用c学习UNIX网络编程,做了一个小的TCP Server-Client程序。
它在 Fedora 上运行完美,但在我的 MacBook 上存在一些连接问题。
编译可以在终端没有问题,但是在我运行“服务器”程序然后运行“客户端”程序之后,似乎“服务器”没有从“客户端”获得“连接”命令。代码和消息如下:
服务器.c:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <errno.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#define MYPORT 1500
#define BACKLOG 5
#define MAXDATASIZE 100
int main()
{
int servSock, cliProc;
socklen_t sin_size;
char buf[MAXDATASIZE], msg[MAXDATASIZE];
struct sockaddr_in my_addr, income_addr;
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if ((servSock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
printf("Socket Error: %d\n", errno);
else
printf("Server Socket %d created\n", servSock);
if (bind(servSock, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
printf("Bind Error: %d\n", errno);
else
printf("Server Bind created\n");
listen(servSock, BACKLOG);
printf("Server is waitting for connection...\n");
sin_size = sizeof(struct sockaddr_in);
if ((cliProc = accept(servSock, (struct sockaddr *)&income_addr, &sin_size)) == -1)
printf("Accept Error: %d\n", errno);
else
{
printf("Server accepted connection from %s\n", inet_ntoa(income_addr.sin_addr));
}
sprintf(msg, "Welcome to Server, you addr is %s", inet_ntoa(income_addr.sin_addr));
send(cliProc, msg, strlen(msg), 0);
if (recv(cliProc, buf, MAXDATASIZE, 0) == -1)
{
printf("Recv Error: %d\n", errno);
}
else
{
printf("Server received %s from %s\n", buf, inet_ntoa(income_addr.sin_addr));
}
close(cliProc);
close(servSock);
printf("Server Sockets closed\n");
return 0;
}
客户端.c:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <errno.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#define MAXDATASIZE 100
#define PORT 1500
int main(int argc, char *argv[])
{
int cliSock, numbytes;
char buf[MAXDATASIZE], msg[MAXDATASIZE];
if (argc != 2)
{
printf("%d, Usage: Client Hostname\n", argc);
exit(1);
}
struct hostent *he;
if ((he = gethostbyname(argv[1])) == NULL)
{
printf("Couldn't get hostname\n");
exit(1);
}
struct sockaddr_in dest_addr;
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(PORT);
dest_addr.sin_addr = *((struct in_addr *)he->h_addr);
if ((cliSock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
printf("Socket Error: %d\n", errno);
else
printf("Client Socket %d created\n", cliSock);
if (connect(cliSock, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr)) == 1)
printf("Connect Error: %d\n", errno);
else
{
printf("Client Connection created\n");
}
numbytes = recv(cliSock, buf, MAXDATASIZE, 0);
buf[numbytes] = '\0';
printf("Received Message: %s\n", buf);
sprintf(msg, "4 8 15 16 23 42");
send(cliSock, msg, MAXDATASIZE, 0);
printf("Client sent %s to %s\n", msg, inet_ntoa(dest_addr.sin_addr));
close(cliSock);
printf("Client Sockets closed\n");
return 0;
}
服务器消息:
Server Socket 3 created
Server Bind created
Server is waitting for connection...
客户留言:
Client Socket 3 created
Client Connection created
Received Message:
任何帮助将不胜感激。:)