Doubt in Socket Program


P

priya

Hi All,

Currently I am working in Socket Progrm written in C.

Here i pasted Both Server and client code..


Server.c code


#include "stdio.h"
#include <winsock.h>
#include <process.h>
#include <windows.h>
#define PORT 1200
#define BACKLOG 4
#include "stdlib.h"


#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "wsock32.lib")
#pragma comment(lib, "advapi32.lib")

int i;

int main()
{
WSADATA wsda;
WSAStartup(0x0101,&wsda);

struct sockaddr_in server;
struct sockaddr_in client;

int sockfd,sockfd2,sockfd3, n_bytes;

char *ret11;
char *a="hello client";
char *b= "Got my Msg";
char *c ="Reply Me ";

const char *msg1;
const char *msg2;
const char *msg3;
char *msg;

msg1 = a;
msg2 = b;
msg3 = c;

if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
printf("Socket error...");
return 0;
}

server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=htons(PORT);
server.sin_family=AF_INET;


if(bind(sockfd,(struct sockaddr*)&server,sizeof(struct
sockaddr))==-1)
{
printf("Cannot bind...");
return 0;
}

if(listen(sockfd,BACKLOG))
{
printf("Error Listening...");
return 0;
}



int size=sizeof(struct sockaddr_in);
if((sockfd2=accept(sockfd,(struct sockaddr*)&client,&size))==-1)
{
printf("Accept Error...");
return 0;
}

send(sockfd2,msg1,sizeof(msg1),0);

send(sockfd2,msg2,sizeof(msg2),0);

send(sockfd2,msg3,sizeof(msg3),0);




if(listen(sockfd,BACKLOG))
{
printf("Error Listening...");
return 0;
}

int size1=sizeof(struct sockaddr_in);
if((sockfd3=accept(sockfd,(struct sockaddr*)&client,&size1))==-1)
{
printf("Accept Error...");
return 0;
}

if((n_bytes=recv(sockfd3,msg,sizeof(msg),0))==-1)
{

printf("Error Recv in receving value from client(hello)
............");

int err = WSAGetLastError();
if(err == WSAECONNRESET)
printf("\n WSAECONNRESET Error\n");
}


ret11=msg;



if(listen(sockfd,BACKLOG))
{
printf("Error Listening...");
return 0;
}


WSACleanup();
closesocket(sockfd);
closesocket(sockfd2);
closesocket(sockfd3);
return 0;

}




I am sending Three message using multiple sends in same server code.
1.is this possible to use multiple send in same server code?



2.client code

#include "stdio.h"
#include <winsock.h>
#include <sys/types.h>
#include "string.h"
#include "stdlib.h"

#define PORT 1200

#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "wsock32.lib")
#pragma comment(lib, "advapi32.lib")



int main(int argc,char *argv[])
{
WSADATA wsda;
WSAStartup(0x0101,&wsda);

struct sockaddr_in server;
static int sockfd;
struct hostent *h;
int i=0;
char *a="hello server";
char *b;
char *c;
char *d;

char *msg,*msg1;
char *msg2;
char *msg3;
char **res;
char *ar[3];
int n_bytes;

if(argc!=2)
{
printf("Usage : client ");
exit(1);
}

if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
printf("Socket Error...");
exit(1);
}

if((h=gethostbyname(argv[1]))==NULL)
{
fprintf(stderr,"Host Name Error...");
exit(1);
}

server.sin_addr=*((struct in_addr*)h->h_addr);
server.sin_port=htons(PORT);
server.sin_family=AF_INET;

if(connect(sockfd,(struct sockaddr*)&server,sizeof(struct
sockaddr))==-1)
{
fprintf(stderr,"Connection out...");
exit(1);
}


if((n_bytes=recv(sockfd,msg1,sizeof(msg1),0))==-1)
{
printf("Error Recv...");
}

printf("the client values is1 %s:\n",msg1);

b=msg1;


if((n_bytes=recv(sockfd,msg2,sizeof(msg2),0))==-1)
{
printf("Error Recv...");
}

printf("the client values is2 %s :\n",msg2);
c=msg2;


if((n_bytes=recv(sockfd,msg3,sizeof(msg3),0))==-1)
{
printf("Error Recv...");
}


printf("the client values is3 %s :\n",msg3);

d=msg3;


ar[0]=b;
ar[1]=c;
ar[2]=d;
res=ar;



msg=a;

if((n_bytes=send(sockfd,msg,sizeof(msg),0))==-1)
{
printf("Error Sending in Client...");
}


closesocket(sockfd);
return 0;
}




Here i am using three recv function for collecting all messege .

My output is

the client values is1 :hell
the client values is2 :Got
the client values is3 :Repl

Here i didnt get the full output what i sent from server side.

Where did i do the mistake?

plz point out my mistake?




Regards,
priya
 
Ad

Advertisements

G

Grumble

priya said:
Currently I am working in Socket Program written in C.

Sockets are outside the scope of this newsgroup.
#include "stdio.h"
#include <winsock.h>
#include <process.h>
#include <windows.h>

It seems you want one of the microsoft.public.* newsgroups.

microsoft.public.win32.programmer.networks perhaps?
 
Ad

Advertisements

T

tigervamp

priya said:
Hi All,

Currently I am working in Socket Progrm written in C.

Here i pasted Both Server and client code..

Networking is not part of Standard C so it is offtopic here.
Server.c code


#include "stdio.h"
#include <winsock.h>
#include <process.h>
#include <windows.h>
#define PORT 1200
#define BACKLOG 4
#include "stdlib.h"

[snip]

You seem to be programming on a Windows platform, maybe one of the
microsoft/ms-windows groups would be more appropriate. See below for
possible insight.
I am sending Three message using multiple sends in same server code.
1.is this possible to use multiple send in same server code?

2.client code

#include "stdio.h"
#include <winsock.h>
#include <sys/types.h>
#include "string.h"
#include "stdlib.h"

#define PORT 1200

#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "wsock32.lib")
#pragma comment(lib, "advapi32.lib")

int main(int argc,char *argv[])
{
WSADATA wsda;
WSAStartup(0x0101,&wsda);

struct sockaddr_in server;
static int sockfd;
struct hostent *h;
int i=0;
char *a="hello server";
char *b;
char *c;
char *d;

char *msg,*msg1;
char *msg2;
char *msg3;
char **res;
char *ar[3];
int n_bytes;

if(argc!=2)
{
printf("Usage : client ");
exit(1);
}

if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
printf("Socket Error...");
exit(1);
}

if((h=gethostbyname(argv[1]))==NULL)
{
fprintf(stderr,"Host Name Error...");
exit(1);
}

server.sin_addr=*((struct in_addr*)h->h_addr);
server.sin_port=htons(PORT);
server.sin_family=AF_INET;

if(connect(sockfd,(struct sockaddr*)&server,sizeof(struct
sockaddr))==-1)
{
fprintf(stderr,"Connection out...");
exit(1);
}


if((n_bytes=recv(sockfd,msg1,sizeof(msg1),0))==-1)


This is most likely where your problem is. I assume that recv has a
signature similiar to:

ssize_t recv(int s, void *buf, size_t len, int flags);

Where s is the socket descriptor, msg1 is the buffer to place the
received data, and len is the length of the buffer. All of this is
off-topic here, BUT, I see two fundamental issues with this statement.
First, you never initialized msg1 so you are invoking undefined
behavior by asking recv to write to where ever it happens to points to.
Second, sizeof(msg1) will return the size of the pointer msg1 (not
sure what you were thinking here), which in your case appears to be 4.
You are only receiving the first 4 bytes of the message which is what
always gets printed. You need to allocate adequate space for your
buffer and provide this length to recv.
{
printf("Error Recv...");
}

printf("the client values is1 %s:\n",msg1);

b=msg1;

if((n_bytes=recv(sockfd,msg2,sizeof(msg2),0))==-1)
{
printf("Error Recv...");
}

printf("the client values is2 %s :\n",msg2);
c=msg2;

if((n_bytes=recv(sockfd,msg3,sizeof(msg3),0))==-1)
{
printf("Error Recv...");
}

printf("the client values is3 %s :\n",msg3);

d=msg3;

ar[0]=b;
ar[1]=c;
ar[2]=d;
res=ar;

msg=a;

if((n_bytes=send(sockfd,msg,sizeof(msg),0))==-1)
{
printf("Error Sending in Client...");
}


closesocket(sockfd);
return 0;
}

Here i am using three recv function for collecting all messege .

My output is

the client values is1 :hell
the client values is2 :Got
the client values is3 :Repl

Here i didnt get the full output what i sent from server side.

Where did i do the mistake?

plz point out my mistake?

Regards,
priya

Please direct any other related questions to a group specific to your
platform.

Rob Gamble
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top