2
분할 할당 오류는 일반적으로 메모리 할당 또는 관리에 오류가있는 경우에 발생합니다. 그러나이 경우 나는 틀린 것이 확실하지 않다. 어떤 제안이라도 도움이 될 것입니다. select()
서버에 연결하려고합니다.세그먼트 오류 : 11, TCP 클라이언트에서
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<netdb.h>
#define INPUT "Socket TCP"
#define BUF_SIZE 1024
int main(int argc, char *argv[]){
int sockt;
struct sockaddr_in serv;
struct hostent *host;
char buff[BUF_SIZE];
serv.sin_family = AF_INET;
/*host = argv[1];
host = malloc (1 + strlen (argv[1]));
*/
host = gethostbyname (argv[1]);
printf("1");
if(host == 0)
{
perror("gethostbyname failed");
close(sockt);
exit(1);
}
else
printf("gethost name succeeded \n");
sockt = socket(AF_INET, SOCK_STREAM, 0);
printf("2");
if(sockt < 0)
{
perror("socket failed");
exit(1);
}
else
printf("socket connected \n");
printf("3");
memcpy(&serv.sin_addr, host->h_addr, host->h_length);
serv.sin_port = htons(1234); /*Convert from host byte order to network byte order*/
printf("4");
/*Condition to check if the client has connected*/
if(connect(sockt, (struct sockaddr *) &serv, sizeof(serv)) <0)
{
perror("Failed to connect");
close(sockt);
exit(1);
}
else
printf("5");
/*Condition to check if the data is sent*/
if(send(sockt, INPUT, sizeof(INPUT), 0) < 0)
{
perror("Failed to send the data");
close(sockt);
exit(1);
}
else
printf("data sent");
printf("The data sent is %s\n", INPUT);
close(sockt);
return 0;
}
'gethostbyname()'이 실패했을 경우'close (socket);는 불확실한'int' 값을 닫습니다. 그 코드 행은 거기에 없어야합니다. 나는 당신이 분명히 적어도 단계별로 디버깅하려고 시도했기 때문에 + 1'ing this 토론하고있다. 솔직하게 대부분의 사람들이 차지하는 것보다 솔직하게 말한'printf'를 뿌렸다. – WhozCraig
나는 이해할 수 없으므로 close socket 문을 제거했지만 여전히 같은 오류가 발생합니다. –
당신이 내 질문에 신경 쓰지 않는다면, 유닉스 소켓 프로그래밍에 대한 일반적인 가이드로 무엇을 사용하고 있습니까? [** Beej 's Guide to Network Programming **] (http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html)은 약간 날짜가 있지만, 당신이 이미 비슷한 것을 가지고 있지 않다면 예제는 매우 도움이 될 수 있습니다. – WhozCraig