我是 C++ 编程的初学者,有一点 Java 经验。我的目标是在服务器和客户端(在两个不同的文件夹中)之间传输文件(在本地主机中)。
我在网上找到了这段适合我的代码,它打开套接字并在服务器和客户端之间建立连接,您可以发送在终端中键入的消息。
我在互联网上尝试了很多不同的代码,要么无法编译,要么不是我需要的。
这是我发现的可以传输消息的代码。
服务器.cpp 文件
enter code here
/* A simple server in the internet domain using TCP
The port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2) {
fprintf(stderr,"ERROR, …
Run Code Online (Sandbox Code Playgroud)