开发者

can anyone tell how to run udp client server program in sunsolaris system? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. 开发者_运维技巧 Closed 11 years ago.

program has no errors but i am unable to execute the prog it is showing segmentation fault can anyone tell the syntax for executing the client server program in linux and sun sorlaris also.


I have a very simple UDP client-server program that's working fine on my Ubuntu machine. It's a very minimal program with no error checking and precautions and it's definitely not the way to write. Server code:


#define MAXLINE 255
#define MYPORT 9100
int main()
{
int listenfd,msglen,clientlen;
char buff[MAXLINE];
struct sockaddr_in servaddr,client;
listenfd=socket(AF_INET,SOCK_DGRAM,0);
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=INADDR_ANY;
servaddr.sin_port=htons(MYPORT);
bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
clientlen=sizeof(client);
printf("Server is listening");
while(1)
{
msglen=recvfrom(listenfd,buff,100,0,(struct sockaddr *)&client,&clientlen);
buff[msglen]='\0';
sendto(listenfd,buff,msglen,0,(struct   sockaddr*)&client,clientlen);
}
}

Client code:


#define MAXLINE 255
#define SERV_PORT 9100
#define SA struct sockaddr
int main()
{
char send[100],recv[100];
int sockfd,len;
struct sockaddr_in servaddr;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(SERV_PORT);
inet_pton(AF_INET,"127.0.0.1",&servaddr.sin_addr);
memset((&servaddr.sin_zero),'\0',8);
while(1)
{
printf("Type your message:");
fgets(send,100,stdin);
sendto(sockfd,send,strlen(send),0,(SA*) &servaddr,sizeof(servaddr));
if((len=recvfrom(sockfd,recv,100,0,(SA*)NULL,NULL))>0)
{
recv[len]='\0';
printf("Server echo:%s\n",recv);
}
}
close(sockfd);
}

Include the necessary header files. sys/types.h sys/socket.h netinet/in.h stdlib.h stdio.h

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜