gpt4 book ai didi

c - C中的TCP套接字编程,其中服务器在cloud9中运行,客户端在本地计算机中运行

转载 作者:行者123 更新时间:2023-11-30 15:07:16 24 4
gpt4 key购买 nike

我对套接字编程完全陌生,所以请耐心等待。我将尝试从本地计算机连接到在 cloud9 中运行的 tcp 服务器。是否可以连接?如果可以,如何连接?

服务器.c

#include<stdio.h>
#include<string.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h>
int main(int argc , char *argv[])
{
int socket_desc , client_sock , c , read_size;
struct sockaddr_in server , client;
char client_message[2000];

//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");

//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("0.0.0.0");
server.sin_port = htons( 8080 );

//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");

//Listen
listen(socket_desc , 3);

//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);

//accept connection from an incoming client
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
puts("Connection accepted");

//Receive a message from client
while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 0 )
{
//Send the message back to client
write(client_sock , client_message , strlen(client_message));
}

if(read_size == 0)
{
puts("Client disconnected");
fflush(stdout);
}
else if(read_size == -1)
{
perror("recv failed");
}

return 0;
}

客户端.c

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <strings.h>
int main()
{
int sock,i;
struct sockaddr_in server;
char message[100] , server_reply[100];
struct hostent *lh = gethostbyname("tcpserver- chaturasan.c9users.io");
*message = '\0';
*server_reply = '\0';
printf("%s %s",message,server_reply);
//Create socket
sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1)
{
printf("Could not create socket");
}
puts("Socket created");
server.sin_addr.s_addr = inet_addr("104.155.229.222");
//bcopy ( lh->h_addr, &(server.sin_addr.s_addr), lh->h_length);
server.sin_family = AF_INET;
server.sin_port = htons(8080);

//Connect to remote server
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
{
perror("connect failed. Error");
return 1;
}

puts("Connected\n");

//keep communicating with server
while(1)
{
printf("Enter message : ");
message[100]='\0';
scanf("%s" , message);

//Send some data
if( send(sock , message , strlen(message) , 0) < 0)
{
puts("Send failed");
return 1;
}

//Receive a reply from the server
if( recv(sock , server_reply , 2000 , 0) < 0)
{
puts("recv failed");
break;
}

puts("Server reply :");
printf("%s",server_reply);
server_reply[100]='\0';
}

close(sock);
return 0;
}

服务器输出是这样的

Socket created
bind done
Waiting for incoming connections...

客户端输出是

 Socket created
Connected
Enter message :

显示客户端已连接,但服务器显示没有连接

最佳答案

Telnet 您的服务器,端口 8080,104.155.229.222。如果您可以连接,那么您的服务器应该输出“连接已接受”(不是在您的 telnet 提示符处,而是在您的服务器消息出现的任何地方)。至少,您会知道您的服务器正在监听您想要的地址/端口。如果您可以连接但没有看到“连接已接受”消息,则您可能正在连接到其他服务器。

关于c - C中的TCP套接字编程,其中服务器在cloud9中运行,客户端在本地计算机中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38370796/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com