- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 C 语言进行 TCP 客户端/服务器通信。客户端必须获取数据的数量(不是 > 6),获取数据并将它们以单个字符串消息的形式发送到服务器,如下所示:4 11 21 9 11.4为数据个数,11、21、9、11为数据。
然后,服务器必须读取消息,打印它并将接收到的数据数(4)发送给客户端。
我为客户试过这个:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main(int argc, char *argv[]) {
int simpleSocket = 0;
int simplePort = 0;
int returnStatus= 0;
char buffsend[256];
char buffrecv[256];
int n, i;
struct sockaddr_in simpleServer;
if (3 != argc) {
fprintf(stderr, "Usage: %s <server> <port>\n", argv[0]);
exit(1);
}
/* create a streaming socket */
simpleSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (simpleSocket == -1) {
fprintf(stderr, "Could not create a socket!\n");
exit(1);
}
else {
fprintf(stderr, "Socket created!\n");
}
/* retrieve the port number for connecting */
simplePort = atoi(argv[2]);
/* setup the address structure */
/* use the IP address sent as an argument for the server address */
//bzero(&simpleServer, sizeof(simpleServer));
memset(&simpleServer, '\0', sizeof(simpleServer));
simpleServer.sin_family = AF_INET;
//inet_addr(argv[2], &simpleServer.sin_addr.s_addr);
simpleServer.sin_addr.s_addr=inet_addr(argv[1]);
simpleServer.sin_port = htons(simplePort);
/* connect to the address and port with our socket */
returnStatus = connect(simpleSocket, (struct sockaddr *)&simpleServer, sizeof(simpleServer));
if (returnStatus == 0) {
fprintf(stderr, "Connect successful!\n");
}
else {
fprintf(stderr, "Could not connect to address!\n");
close(simpleSocket);
exit(1);
}
/* get the message from the server */
do {
printf("get numb of data or terminate(write '0'): ");
fgets(buffsend,256,stdin);
n=atoi(buffsend);
if(n>6) {
printf("Error\n");
}
else {
for(i=0;i<n;i++) {
printf("insert number: ");
fgets(buffsend,256,stdin);
write(simpleSocket, buffsend, strlen(buffsend)+1);
}
} while((n!=0) && (n>0));
close(simpleSocket);
return 0;
}
这对于服务器:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
int simpleSocket = 0;
int simplePort = 0;
int returnStatus = 0;
char buff[256];
char message[256];
int count;
struct sockaddr_in simpleServer;
if (2 != argc) {
fprintf(stderr, "Usage: %s <port>\n", argv[0]);
exit(1);
}
simpleSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (simpleSocket == -1) {
fprintf(stderr, "Could not create a socket!\n");
exit(1);
}
else {
fprintf(stderr, "Socket created!\n");
}
/* retrieve the port number for listening */
simplePort = atoi(argv[1]);
/* setup the address structure */
/* use INADDR_ANY to bind to all local addresses */
memset(&simpleServer, '\0', sizeof(simpleServer));
simpleServer.sin_family = AF_INET;
simpleServer.sin_addr.s_addr = htonl(INADDR_ANY);
simpleServer.sin_port = htons(simplePort);
/* bind to the address and port with our socket */
returnStatus = bind(simpleSocket,(struct sockaddr *)&simpleServer,sizeof(simpleServer));
if (returnStatus == 0) {
fprintf(stderr, "Bind completed!\n");
}
else {
fprintf(stderr, "Could not bind to address!\n");
close(simpleSocket);
exit(1);
}
/* lets listen on the socket for connections */
returnStatus = listen(simpleSocket, 5);
if (returnStatus == -1) {
fprintf(stderr, "Cannot listen on socket!\n");
close(simpleSocket);
exit(1);
}
while (1)
{
struct sockaddr_in clientName = { 0 };
int simpleChildSocket = 0;
int clientNameLength = sizeof(clientName);
/* wait here */
simpleChildSocket = accept(simpleSocket,(struct sockaddr *)&clientName, &clientNameLength);
if (simpleChildSocket == -1) {
fprintf(stderr, "Cannot accept connections!\n");
close(simpleSocket);
exit(1);
}
/* handle the new connection request */
/* write out our message to the client */
while(read(simpleChildSocket, buff, 256))
cont++;
printf("%s", buff);
printf("Data received: %d %s\n", cont, buff);
close(simpleChildSocket);
}
close(simpleSocket);
return 0;
}
最佳答案
问题在于客户端在每个值之后发送一个字符串终止符 (\0
),因此当服务器打印整个消息时,它在第一个值之后停止打印。
这个特殊问题可以通过在发送时不包括字符串终止符来解决:
write(simpleSocket, buffsend, strlen(buffsend)); /* Removed the +1 */
接收消息时需要添加字符串结束符:
int length;
char buff[256];
while((length = read(simpleChildSocket, buff, 256-1)) > 0) {
buff[length] = `\0`;
cont++;
printf("Data received: %d %d %s\n", cont, length, buff);
}
另请注意,您正在使用基于流的套接字。这意味着套接字本身没有消息协议(protocol)的概念。参见 this question获取更多信息。
关于c - 发送包含多个数字的字符串 客户端/服务器 TCP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43936750/
这个问题在这里已经有了答案: Prolog - count repetitions in list (3 个答案) 关闭 7 年前。 所以我正在尝试创建一种方法来确定列表中 N 的数量。我已经试验了
使用 sscanf 或任何其他命令从分号后的文件读取的最佳方法是什么,例如,如果我的文件有 5: 4 5 6 7。如何将冒号后的值存储在数组中。此外,分号后面的整数数量可能会有所不同,即在上面给出的示
我正在尝试返回第 n 个数字。如果数字是 3 或 7 的倍数,则从 1 开始,则跳过该数字并获取下一个数字。但是,如果数字是 3 和 7 的倍数,则不会跳过该数字。 public int Multip
如何有效地从末尾获取一定数量的元素? 1 looks like 2 three!! 例如,如何获取最后 2 个 div 的内容? 最佳答案 $(document).ready(function(){
//Generate Food Personality for(i=0; i
我试图在给定的排序数组中找到最大的 K 个数。 例如:输入 -> [ 5, 12, 45, 32, 9, 20, 15]输出 -> K = 3, [45, 32, 20] 到目前为止我编写的代码返回最
两个数字表 a 和 b 被写入并按升序合并在一起,并删除重复项。现在的问题是在这个 super 表中找到比 O(n) 复杂度更好的 nth 数。 Limits 1 #include using nam
给定一个包含 N 个元素的数组 A,我需要找到对 (i,j) 使得 i 不等于 j 并且如果我们为所有对 (i, j) 然后它来到第k个位置。 示例:让 N=4 和数组 A=[1 2 3 4] 如果
给定一组跳过的数字,我需要找到该组中不存在的第 N 个数字。示例: 给定一组 [1, 4, 5] 一些结果: 对于 N = 1 结果 0 对于 N = 2 结果 2(因为 1 被跳过) 对于 N =
几个月前在亚马逊的招聘挑战中遇到了这个问题。 给定两个数字 a 和 b 及其倍数的升序列表,找出第 n 个倍数。 例如,如果 a = 4 , b = 6 和 n = 6 那么答案是 18因为列表是 4
所以我最近一直在研究 Python,我试图找到一种方法来在单个表达式中输出斐波那契数列的第 n 个数。这是我到目前为止编写的代码: (lambda f: f if f 1 # n == 2 -> 1
作业是编写一个 C++ 程序,它接受输入数字 n 并输出序列中的第 n 个数字: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 ... 这是我到目前为止想出的:
问题很简单(答案很可能):如何找到数组中最小的 2 个数字? for ( i = 1; i 关于c++ - 数组中最小的 2 个数,我们在Stack Overflow上找到一个类似的问题: ht
您可以调用Nokogiri::XML::Node#ancestors.size 来查看节点的嵌套深度。但是有没有办法确定嵌套最深的子节点的嵌套深度呢? 或者,您如何找到从一个节点下降的所有叶节点? 最
这个任务是找到n个数字的fibanocci。任务: 1.找出n个数的斐波那契数。 2.使用变量n,first=0,second=1,next,c。输入格式:使用 printf 语句。使用 scanf
我想添加每 10 个元素的数量。 例如, function myFunction() { for (var i = 1; i "; } } 输出: 1,2,3,4,5,6,7,8,9,
我想编写一个程序来计算斐波那契数列的第 n 个数,这是我使用 printf 和 scanf 完成的。但我希望更改我的程序,以便在命令行中输入序列号,而不是在程序提示时输入。这就是我想出的。它可以编译,
我有一个方案中的对象列表。每个对象都与一个可以在运行时计算的置信度值相关联。我想找到具有最高置信度值的前 50 个此类对象。示例:((WordPair1) (WordPair2)) 等等都是我的对象。
我正在寻找一种给定目标的算法,返回目标位为 0 的第 N 个数字。 例如,对于n={0,1,2,3}和target=1的输入,输出将是(二进制) 000,001,100,101 最佳答案 只写值N-1
我正在尝试创建一个函数来获取 vector 中的 3 个最大数字。例如:数字:1 6 2 5 3 7 4结果:5 6 7 我想我可以对它们进行 DESC 排序,在开始时获取 3 个数字,然后再对它们进
我是一名优秀的程序员,十分优秀!