Posts Tagged ‘Client’

Can Anyone Help Me With The Hello World Server And Client Program?

Tuesday, February 9th, 2010

I’m suppose to do this program and make it work in windows os and linux but i can’t seem to make it run. i always get an error. can you tell me wats wrong? should i include some more commands? i’m new in this socket programming thing and i really need help.
here’s the program:
#include
#include /* exit() */
#include /* memset (), memcpy()*/
#include /* uname()*/
#include
#include /* socket(), bind(), listen(),accept()*/
#include
#include
#include
#include /* fork(), write(), close()*/
int_GetHostName(char *buffer, int length);
const char MESSAGE[] = “Hello, World!\n”;
const int BACK_LOG=5;
int main(int argc, char *argv[]){
int serverSocket=0, on=0, port=0,status=0,childPid=0;
struct hostent *hostPtr=NULL;
char hostname[80]=”";
struct sockaddr_in serverName={0};
if (2!=argc){
fprintf(stderr,”Usage: %s \n”,argv[0]);
}
port=atoi(argv[1]);
serverSocket=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(-1==serverSocket){
perror(“socket()”);
exit;
}
/* turn off bind address checking, and allow port numbers to be reused –
otherwise the TIME_WAIT phenomenon will prevent binding to these address.port
combinations for (2*MSL) seconds.
*/
on=1;
status=setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&on,sizeof(on));
if(-1==status){
perror(“setsockopt(…,SO_REUSEADDR,…
}
/* when connection is closed, thereis a need to linger to ensure all data is transmitted,
so turn this on also
*/
{
struct linger linger={0};
linger.1_onoff=1;
linger.1_linger=30;
status=setsockopt(serverSocket,SOL_S… char*)&linger,sizeof(linger));
if(-1==status){
perror(“setsockopt(…,SO_LINGER,..…
}
}
// FIND OUT WHO I AM
status=_GetHostName(hostname,sizeof(h…
if(-1==status){
perror(“_GetHostName()”);
exit(1);
}
hostPtr=gethostbyname(hostname);
if(NULL==hostPtr){
perror(“gethostbyname()”);
exit(1);
}
(void)memset(&serverName,0,sizeof(ser…
(void)memcpy(&serverName.sin_addr,hos…
/* to allow server be contactable on any of its IP addresses, uncomnment the following line of code
serverName.sin_addr=hton1(INADDR_ANY…
*/
serverName.sin_family=AF_INET;
serverName.sin_port=htons(port); //network order
status=bind(serverSocket,(structsocka…
if(-1==status){
perror(“bind()”);
exit(1);
}
status=listen(serverSocket,BACK_LOG);
if(-1==status){
perror(“listen()”);
exit(1);
}
for(;;){
struct sockaddr_in clientName={0};
int slaveSocket, clientLength=sizeof(clientName);
(void)memset(&clientName,0,sizeof(cl…
slaveSocket=accept(serverSocket,(str… sockaddr*)&clientName,&clientLength);
if(-1==slaveSocket){
perror(“accept()”);
exit(1);
}
childPid=fork();
switch(childPid){
case -1: /* ERROR */
perror(“fork()”);
exit(1);
case 0: /* child process */
close(serverSocket);
if(-1==getpeername(slaveSocket,(s… sockaddr*)&clientName,&clientLength)){
perror(“getpeername()”);
}else{
printf(“Connectionrequest from %s \n”; , inet_ntoa(clientName.sin_addr));
}
/* Server application spoecific code goes here,
e.g.perform some action, respond to client etc.
*/
write(slaveSocket,MESSAGE,strlen(…
close(slaveSocket);
exit(0);
default: /*parentprocess*/
close(slaveSocket);
}
}
return 0;
}
/* Local replacement of gethostname() to aid portability */
int _GetHostName(char *buffer, int length){
struct utsname sysname={0};
int status=0;
status=uname(&sysname);
if(-1!=status){
strncpy(buffer,sysname.nodename,len…
}
return(status);
}
please help..

How To Install A Dll On A Client Machine Using Asp Or Javascript?

Sunday, January 31st, 2010

I want a DLL to be installed on a client machine so that i can use that DLL to perform some work

Hi I A Writting Simple Server Client Socket.the Code That I Have Written Is Not Working.please Anyone Help Me?

Friday, January 15th, 2010

#include
#include /* Basic I/O routines */
#include /* standard system types */
#include /* Internet address structures */
#include /* socket interface functions */
#include /* host to IP resolution */
#include
#define portno 6789 /* default port of host */
void error(char *msg);
int main(int argc, char *argv[]);
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int serversocket;
int newsockfd;
int clilen;
char buffer[4096];
struct sockaddr_in serv_addr, cli_addr;
/*portno = 80;*/
int n;
serversocket = socket(AF_INET, SOCK_STREAM, 0);
if (serversocket < 0)
{
error("ERROR opening socket");
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
/*portno=atoi(argv[1]);*/
serv_addr.sin_port = htons(portno);
printf("Socket has port #%d\n", serv_addr.sin_port);
if (bind(serversocket, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
{
error("ERROR on binding");
}
listen(serversocket,5);
while (1) {
clilen = sizeof(cli_addr);
newsockfd = accept(serversocket,(struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
{
error("ERROR on accept");
}
/*n = read(newsockfd,buffer,sizeof(buffer)-1);
printf("A connection has been established\n");
if (n < 0)
{
error("ERROR reading from socket");
}
printf("Here is the message: %s\n",buffer);*/
n = write(newsockfd, buffer, sizeof(buffer)-1);
if (n < 0)
{
error("ERROR writing to socket");
}
close(newsockfd);
}
return 0;
}

Can Someone Help Me With Client Server Error?

Sunday, September 20th, 2009

i am trying to complie this client.c using gcc -lsocket client.c
but i get these error:
warning: incompatible implicit declaration of built-in function ‘bzero’
warning: incompatible implicit declaration of built-in function ‘bcopy’
passing argument 2 of ‘connect’ from incompatible pointer type
warning: incompatible implicit declaration of built-in function ’strlen’
here is the code below:
#include
#include
#include
#include
#include
#include
void error(char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");