我想使用串行COM端口进行通信,我希望每次调用read函数调用时都实现超时.
int filedesc = open( "dev/ttyS0", O_RDWR );
read( filedesc, buff, len );
Run Code Online (Sandbox Code Playgroud)
编辑:
我正在使用Linux操作系统.如何使用select函数调用实现?
我想编写一个C程序来生成Get Request而不使用任何外部库.这可能只使用C库,使用套接字吗?我正在考虑制作一个http数据包(使用正确的格式)并将其发送到服务器.这是唯一可行的方式还是有更好的方法?
我试图在服务器和客户端之间进行文件传输,但工作非常糟糕.基本上需要发生的是:
1)客户端将txt文件发送到服务器(我称之为"quotidiani.txt")
2)服务器将其保存在另一个txt文件中("receive.txt")
3)服务器运行它上面的一个脚本修改它并用另一个名字保存它("output.txt")
4)服务器将文件发送回客户端,保存它(在同一个套接字上)名称(final.txt)
问题是第一个文件(quotidi.txt)只是读取了一小部分,然后有一些错误.我希望有人帮助我理解并纠正我的错误.
这是我的代码:
client.c:
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <signal.h>
#include <ctype.h>
#include <arpa/inet.h>
#include <netdb.h>
#define PORT 20000
#define LENGTH 512
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
/* Variable Definition */
int sockfd;
int nsockfd;
char revbuf[LENGTH];
struct sockaddr_in remote_addr;
/* Get the Socket file descriptor */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) …Run Code Online (Sandbox Code Playgroud) 这是服务器(sendfile)部分:
offset = 0;
for (size_to_send = fsize; size_to_send > 0; ){
rc = sendfile(newsockd, fd, &offset, size_to_send);
if (rc <= 0){
perror("sendfile");
onexit(newsockd, sockd, fd, 3);
}
offset += rc;
size_to_send -= rc;
}
close(fd); /* la chiusura del file va qui altrimenti rischio loop infinito e scrittura all'interno del file */
memset(buffer, 0, sizeof(buffer));
strcpy(buffer, "226 File Successfully transfered\n");
if(send(newsockd, buffer, strlen(buffer), 0) < 0){
perror("Errore durante l'invio 226");
onexit(newsockd, sockd, 0, 2);
}
memset(buffer, 0, sizeof(buffer));
Run Code Online (Sandbox Code Playgroud)
这是客户端(recv文件)部分的一部分: …
我正在编写一个小型的C程序,以便能够使用TCP/IP套接字在两台计算机之间(从运行Linux的服务器到客户端)传输图像文件,但由于我的图片出现在另一侧,似乎出现了错误损坏.
我的服务器的代码是这样的:
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<iostream>
#include<fstream>
#include<errno.h>
using namespace std;
int send_image(int socket){
FILE *picture;
int size, read_size;
char send_buffer[10240], verify;
picture = fopen("2.jpg", "r");
printf("Getting Picture Size\n");
if(picture == NULL) {
printf("Error Opening Image File");
}
fseek(picture, 0, SEEK_END);
size = ftell(picture);
fseek(picture, 0, SEEK_SET);
//Send Picture Size
printf("Sending Picture Size\n");
write(socket, (void *)&size, sizeof(int));
if(read_size = read(socket, &verify , sizeof(char)) < 0) {
puts("\nError Receiving Verification");
}
if(verify == '1'){
printf("5\n");
//Send Picture as Byte …Run Code Online (Sandbox Code Playgroud) 我在Linux上尝试TCP文件传输.建立连接后,服务器应将"send.txt"发送到客户端,客户端接收该文件并将其保存为"receive.txt".然后连接中断.
正确的输入和输出应该是:
服务器终端:
$./server &
[server] obtain socket descriptor successfully.
[server] bind tcp port 5000 in addr 0.0.0.0 successfully.
[server] listening the port 5000 successfully.
[server] server has got connect from 127.0.0.1.
[server] send send.txt to the client…ok!
[server] connection closed.
Run Code Online (Sandbox Code Playgroud)
客户终端:
$./client
[client] connected to server at port 5000…ok!
[client] receive file sent by server to receive.txt…ok!
[client] connection lost.
Run Code Online (Sandbox Code Playgroud)
服务器和客户端都应该在进程后退出.
但是我现在得到了什么
$ ./server &
[server] obtain socket descriptor successfully.
[server] bind tcp port 5000 in addr 0.0.0.0 sucessfully. …Run Code Online (Sandbox Code Playgroud) 我写的TCP中继服务器就像对等路由器(超级节点)一样工作.
最简单的情况是两个打开的套接字和它们之间的数据中继:
clientA <---> server <---> clientB
然而,服务器必须服务大约2000个这样的AB对,即.4000个插座......
userland中有两个众所周知的数据流中继实现(基于socketA.recv() - > socketB.send()和socketB.recv() - > socketA.send()):
我使用线程,所以在最坏的情况下服务器创建2*2000个线程!我不得不限制堆栈大小,它的工作原理,但它是正确的解决方案吗?
我的问题的核心:
有没有办法避免用户区中两个套接字之间的活动数据中继?
似乎有一种被动的方式.例如,我可以从每个套接字创建文件描述符,创建两个管道并使用dup2() - 与stdin/out重定向相同的方法.然后两个线程对数据中继没用,可以完成/关闭. 问题是服务器是否应该关闭套接字和管道以及如何知道管道何时损坏以记录事实?
我也发现了"套接字对",但我不确定它是否符合我的目的.
你会建议什么解决方案来卸载用户空间并限制线程数量?
一些额外的解释:
感谢Gerhard Rieger,我有一个提示:
我知道有两种内核空间方法可以避免在用户空间中进行读/写,recv/send:
- 发送文件
- 拼接
两者都有关于文件描述符类型的限制.
dup2无助于在内核中执行某些操作,AFAIK.
手册页:splice(2) splice(2) vmsplice(2) sendfile(2) tee(2)
相关链接:
我正在为学校项目编写一个简单的FTP服务器.项目几乎完成,我唯一的问题是通过套接字向客户端发送文件.我无法写入超过200kb数据的套接字,成功下载小文件.谁能告诉我通过Linux套接字发送大文件的正确方法是什么?
提前致谢.
PS我使用C和32位linux,服务器在PORT模式下工作,一直使用低级开放,写入,读取等功能,如sendfile,send,sendto.
我的数据自然是分段的,并且来自不同的来源,因此我writev按如下方式使用该函数。
char *buf0 = "short string\n";
struct bar *my_bar;
my_baz = get_my_baz();
iov[0].iov_base = buf0;
iov[0].iov_len = strlen(buf0);
iov[1].iov_base = my_bar;
iov[1].iov_len = sizeof (struct bar);
bytes_written = writev (sockfd, iov, 3);
Run Code Online (Sandbox Code Playgroud)
我想发送我的所有数据。如果 writev 一次只写入部分数据怎么办,例如:
bytes_written = 1;
Run Code Online (Sandbox Code Playgroud)
如何继续发送剩余数据?在write函数中,我们可以使用这个方法,那么writev呢?
您已经通过c ++中的客户端/服务器连接实现了简单的文件交换.工作得很好,除了一个很慢的问题.这是我的代码:
用于发送文件:
int send_file(int fd)
{
char rec[10];
struct stat stat_buf;
fstat (fd, &stat_buf);
int size=stat_buf.st_size;
while(size > 0)
{
char buffer[1024];
bzero(buffer,1024);
bzero(rec,10);
int n;
if(size>=1024)
{
n=read(fd, buffer, 1024);
// Send a chunk of data
n=send(sockFile_, buffer, n, 0 );
// Wait for an acknowledgement
n = recv(sockFile_, rec, 10, 0 );
}
else // reamining file bytes
{
n=read(fd, buffer, size);
buffer[size]='\0';
send(sockFile_,buffer, n, 0 );
n=recv(sockFile_, rec, 10, 0 ); // ack
}
size -= …Run Code Online (Sandbox Code Playgroud)