C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-21-2009, 07:47 PM   #1
Registered User
 
Join Date: Apr 2009
Posts: 4
Problem with client server program...

Hi Guys... I am trying to run client server program, but the server program is giving me problem... after the client connects to it.. it says Illegal instruction and logs out... i am relatively new to this.. so please bare with me.. i have done lot of research on net, could not find anything that was useful to me... please bail me out!!!!! :-(

Server program

Code:
#include<stdio.h>
#include<string.h>
#include<sys/types.h> 
#include<errno.h>
#include<sys/socket.h> 
#include<netdb.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<unistd.h>

#define PORT 6090


int main()
{
       int client_lenght;
       struct sockaddr_in client,server;
       struct hostent *ptrh;
       int sd,sd2;
       char message[50];
       int len;
       memset((char *)&server,0,sizeof(server));
       server.sin_family=AF_INET;
       server.sin_addr.s_addr=INADDR_ANY;
       server.sin_port = htons(PORT);
       sd = socket(AF_INET,SOCK_STREAM,0);
       if(sd<0)
      {
          fprintf(stderr,"cannot create socket\n");
          exit(1);
       }

      if(bind(sd,(struct sockaddr *)&server, sizeof(server))<0)
      {
         fprintf(stderr,"bind failed\n");
         exit(1);
       }

       listen(sd,5);
      while(1)
     {
       len = sizeof(client);

       if((sd2=accept(sd,(struct sockaddr *)&client, &len))<0)
              {
                   fprintf(stderr,"Accept failed\n");
                   exit(1);
              }

       printf("Waiting for message from client\n");
       recv(sd2,message,sizeof(message),0);
       printf("Meesage is %s\n",message);
       close(sd2);
     }
    return 0;
}

Client Program

Code:
#include<stdio.h>
#include<string.h>
#include<sys/types.h> 
#include<errno.h>
#include<sys/socket.h> 
#include<netdb.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<unistd.h>

#define PORT 6090

int main()
{
     int client_lenght;
     struct sockaddr_in client,server;
     struct hostent *ptrh;
     int sd;
    char message[50];
    memset((char *)&server,0,sizeof(server));
    memset((char *)&client,0,sizeof(client));
    server.sin_family = AF_INET;


/*
ptrh = gethostbyname(host);
if(((char *)ptrh)==NULL)
{
fprintf(stderr,"invalid host: %s\n", host);
exit(1);
}
memcpy(&server.sin_addr,ptrh->h_addr,ptrh->h_length);*/

       server.sin_addr.s_addr=INADDR_ANY;
       server.sin_port		= htons(PORT);
       if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
	{
		fprintf(stderr,"Cannot create socket\n");
		exit(1);
	}
        if(connect(sd,(struct sockaddr *)&server, sizeof(server))<0)
        {
           fprintf(stderr, "connect failed\n");
           exit(1);
       }
	printf("Enter the message to be sent to server: ");
	scanf("%s",&message);

        send(sd, message,50,0);
	fflush(stdout);

	/* Closing the socket*/	
	close(sd);
}
Any help is appreciated........

Last edited by cprogczar; 04-21-2009 at 08:21 PM.
cprogczar is offline   Reply With Quote
Old 04-21-2009, 07:52 PM   #2
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,640
What says illegal instruction? Are you getting a seg fault? Also, please fix your indentation. Hard to read means no one bothers to read it.


Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 04-21-2009, 07:56 PM   #3
Registered User
 
Join Date: Apr 2009
Posts: 4
Yup.. sometimes it says seg fault.. and sometimes it says bus error...


[will fix the indentation for next post.. thanks..]
cprogczar is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Server and client in the same program smithx Networking/Device Communication 1 11-25-2008 09:30 PM
c program client server steve1_rm Networking/Device Communication 3 01-24-2008 10:33 AM
my server program auto shut down hanhao Networking/Device Communication 1 03-13-2004 10:49 PM
Server Client Messaging Program X PaYnE X Networking/Device Communication 3 01-04-2004 05:20 PM
Server client program help please. XiReDDeViLiX C++ Programming 15 04-03-2002 09:21 PM


All times are GMT -6. The time now is 01:29 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22