- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我尝试编写一个连接到 beej 服务器流的简单客户端到目前为止,我写的所有内容都运行良好。现在我想向客户端添加发送他从用户那里获得的数据的能力,但我不知道该怎么做(不使用 scanf ofc)。我该怎么做?这是我的代码:
// chat client
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h> //inet_addr
#include <netinet/in.h>
#include <string.h>
#include <signal.h>
#define PORT 9034 // defined port like the server
void main()
{
char * msg = "omri is here";
char buf[256];
int len = strlen(msg);
int byte_sent;
int socket_dect; // creating socket descriptor
struct sockaddr_in ServerInfo;
// creating a new socket, its a number represent a file descriptor
// socket args : 1)ip protocol ipv4,second tcp/udp, third is number of protocol used
socket_dect = socket(AF_INET,SOCK_STREAM,0);
if(socket_dect == -1){
perror("error creating socket");
}
// fill the values of the server
ServerInfo.sin_family = AF_INET; // ipv4
ServerInfo.sin_port = htons(PORT); // port number
//ServerInfo.sin_addr = 127.0.0.1;
inet_pton(AF_INET, "127.0.0.1", &ServerInfo.sin_addr);//insert the ip to the sin addr
// making the connection to the server
//ServerInfo.sin_addr.s_addr = inet_addr("127.0.0.1"); // another way to put ip addr
connect(socket_dect,(struct sockaddr *)&ServerInfo,sizeof(ServerInfo)); // connected to the server
//signal(SIGALRM,sigAlarm);
// seng data
if(send(socket_dect,msg,len,0) < 0){
perror("send connection");
printf("send error");
}
if(recv(socket_dect,buf,len,0) < 0 ){
printf("recv error");
}
printf("the data reviced from the server is :%s\n",buf);
}
最佳答案
char *input(char *output)
{
char *buffer = NULL;
size_t size = 0;
int count = 0;
printf("%s", output);
count = getline(&buffer, &size, stdin);
buffer[count-1] = '\0';
return buffer;
}
char *msg=input("Enter the message");
之后使用发送。
编辑:您想实现我在上面写的这个功能。之后你将进行一个 while 循环:
while(1)
{
msg=input("Enter the message");
send(sock, msg, strlen(msg), 0);
}
这将允许您无限循环地输入消息并将其发送到服务器。
关于c - linux c client - 添加发送数据的能力,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37192761/
我到处搜索 .net 3.0 和 3.5 框架的内容列表,因为我一直在使用哈希表等旧技术而不是字典(较新的技术)进行编程。 我一直在犹豫,想知道在哪里可以找到 C# 和 .Net 框架的所有最新功能的
你好我有一个非常不寻常的问题,因为我认为在我的情况下工作流运行时没有使用足够的 CPU 能力。场景如下: 我向队列发送了很多消息。我使用 WorkflowRuntime 类中的 EnqueueItem
我正在为 OSX 图像处理应用程序编写一个插件接口(interface)。它的设计理念是插件应该尽可能简单,以便开发人员可以在看到示例代码后几分钟内进入并编写效果。为此,这些示例不需要或使用 XCod
...或者必须抛出异常才能影响性能? 引用已接受的答案here .这是我想要实现的东西,如果以这种方式创建实例不会使事情变得非常慢的话。 (我每秒要创建数千个对象,粒子系统等。所以不,考虑到在 Jav
我必须开发 C API 来通过 UI APPLICATION 使用重启功能而不是系统调用和 exec 功能来关闭 linux 系统。 This reboot link说为了运行这个函数调用者必须有 C
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
Droid SDK 是否支持手指在屏幕上滑动返回上一屏幕,而不是使用返回键? 最佳答案 我假设你的意思是“滑动”作为 i-phone 的解锁系统。 返回上一屏幕没有内置任何东西..用于实现手指检测实现
我无法理解 Michael Hartl 的 Ruby on Rails 教程中“记住我”的实现。他创建了一个带有登录方法的 SessionsHelper 模块,其中包含以下内容: module Ses
近日,华为 分析服务 6.9.0版本发布,正式上线 探索能力 。开发者可自由定义与配置分析模型,支持报告实时预览,数据洞察体验更加灵活与便捷. 新上线的探索能力中,有漏斗分析、事件归因、会话路径分析
给定一个带有属性“日期”的 Rails 模型“Widget”,是否可以在要求日期晚于当前时间的 CanCan 操作授权上设置条件? documentation显示如何指定数字属性的范围: can :r
我有兴趣将 CPU 密集型 Web 应用部署到 Azure 应用服务实例。我找不到有关 Azure 应用服务的 CPU 使用率和/或限制的任何详细信息。我担心的是,如果不深入了解应用程序的 CPU 规
我的场景: 我导航到登录页面。我输入了一个已知的用户名和一个错误的密码。ZAP 没有发现任何问题。 我选择 POST 到登录页面。我找到包含用户名和密码的行。密码:ctl00%24ContentPla
我是 Laravel 的新手,我曾经在 codeigniter 上工作。我对 Eloquent ORM 的概念着迷。我有一个关于 Eloquent 能力的一般性问题。ORM的能力是什么,我的意思是我们
我正在尝试运行 Java Jetty 应用程序,但我一直看到此错误: Caused by: org.postgresql.util.PSQLException: ERROR: function cry
我正在创建一个 UITableView,我将在其中加载并显示博客提要中的数据。该数据将每天、在启动时或动态更新。 我想要做的是为此屏幕提供一些离线功能,这样如果用户没有连接到互联网,他仍然可以看到 V
我使用 ChromeDriver 2.33 和 WebDriver 3.6.0 并尝试设置文件下载的默认目录。 Map prefs = new HashMap(); prefs.put("downlo
我想知道 Java 是否有任何方法可以提示用户输入管理密码,就像他们使用 sudo 时那样,这样我就可以在 Java 程序中运行命令,而不必对系统或 Sudoers。我已经看到在 Python 中完成
Apple Pay 权利/能力仅在 App Store 中有效。我将在 App Store 和 Enterprise 中发布此应用。 如果选择了 App Store Release 方案,我希望 Xc
我正在研究 termcap 库。我正在尝试在终端中做一个行编辑器。我有一个可以在线移动的光标。一条线一切都很好。但是,如果我的行大于终端宽度,我无法使用 le 命令(将光标向左移动)将光标从第二行返回
我有时用 Java 编写代码,我注意到有时它在多核机器上使用超过 100% 的 CPU。我现在在一台有 33 个 CPU(亚马逊的 EC2)的多核机器上运行一些代码,我想让我的 Java 进程使用所有
我是一名优秀的程序员,十分优秀!