Cでサーバーを作成する方法を学んでいますが、別の端末からこのサーバーにtelnetで接続しようとすると、「セグメンテーション違反:11」エラーが発生し続けます。どんな助けでも大歓迎です!
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
void error(char *msg)
{
fprintf(stderr, "%s: %s\n", msg, strerror(errno));
exit(1);
}
int main(int argc, char *argv[])
{
char *advice[] = {
"Take smaller bites\r\n",
"Go for the tight jeans. No they do NOT make you look fat.\r\n",
"One word: inappropriate\r\n",
"Just for today, be honest. Tell your boss what you *really* think\r\n",
"You might want to rethink that haircut\r\n"
};
int listener_d = socket(PF_INET, SOCK_STREAM, 0);
struct sockaddr_in name;
name.sin_family = PF_INET;
name.sin_port = (in_port_t)htons(30000);
name.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(listener_d, (struct sockaddr *) &name, sizeof(name))== -1)
error("Cannot bind");
if (listen(listener_d, 10) == -1)
error("Cannot listen");
puts("Waiting connection");
while(1)
{
struct sockaddr_storage client_addr;
unsigned int address_size = sizeof(client_addr);
int connect_d = accept(listener_d, (struct sockaddr *) &client_addr, &address_size);
char msg = advice[0];
if (send(connect_d, msg, strlen(msg), 0) == -1)
error("Cannot send");
close(connect_d);
}
return 0;
}