Socket lukker ikke forbindelse
Har foelgende kode:#define PORT 8088 // the port client will be connecting to
#define MAXDATASIZE 100 // max number of bytes we can get at once
void x10send(char *hostname)
{
int len,sd,status,tmp,n,ix,noblock,postlen;
int done = 0;
char *message = (char *)malloc(8),ownhost[100],resp[30];
struct sockaddr local,remote;
struct hostent *hostinfo;
noblock=1;
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd<0) {
printf("Error creating socket: %s\n",strerror(errno));
goto slut;
}
local.sa_family=AF_INET;
memset(local.sa_data,0,sizeof(local.sa_data));
status=bind(sd,&local,sizeof(local));
if(status<0) {
printf("Error binding socket: %s\n",strerror(errno));
goto slut;
}
hostinfo=gethostbyname(hostname);
if(!hostinfo) {
printf("Error looking up host: %s\n",hostname);
goto slut;
}
remote.sa_family=hostinfo->h_addrtype;
memcpy(remote.sa_data+2,hostinfo->h_addr_list[0],hostinfo->h_length);
*((short *)remote.sa_data)=PORT;
tmp=remote.sa_data[0];
remote.sa_data[0]=remote.sa_data[1];
remote.sa_data[1]=tmp;
status=connect(sd,&remote,sizeof(remote));
if(status!=0) {
printf("Error connecting to host: %s port: %d\n",hostname,PORT);
goto slut;
}
sleep(1);
// read response
printf("Sending message\n");
postlen=8;
gethostname(ownhost,sizeof(ownhost));
sprintf(message,"%c%c%c009LM",0x21,0x21,0x21);
status=send(sd,message,8,0);
// sleep(1);
// read response
ix=0;
while ((len=recv(sd,resp+ix,sizeof(resp)-ix-1,0))>0) {
ix = ix + len;
}
resp[ix]='\0';
printf("%s\n",resp);
slut:
free(message);
close(sd);
return;
}
int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr,"usage: client hostname\n");
exit(1);
}
x10send(argv[1]);
printf("Done !\n");
return 0;
}
Problemet er at close(sd); ikke lukker forbindelsen paa serveren.
Forslag ?