0% found this document useful (0 votes)
68 views2 pages

Client-Server Chat Program Using Udp Server Program

The document describes a client-server program for chat using UDP. The server program creates a UDP socket, binds it to an address and port, receives data from the client and prints it. The client program creates a socket, sends data entered by user to the server address and port, and prints that data has been sent. The programs successfully exchange the message "hi.." between the client and server using UDP sockets.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views2 pages

Client-Server Chat Program Using Udp Server Program

The document describes a client-server program for chat using UDP. The server program creates a UDP socket, binds it to an address and port, receives data from the client and prints it. The client program creates a socket, sends data entered by user to the server address and port, and prints that data has been sent. The programs successfully exchange the message "hi.." between the client and server using UDP sockets.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

EXPT NO: DATE: CLIENT-SERVER CHAT PROGRAM USING UDP SERVER PROGRAM #include<string.h> #include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.

h> int main() { int sock,name_len,client_add_size; char buff[20]; struct sockaddr_in server,client; sock=socket(AF_INET,SOCK_DGRAM,0); if(sock==-1) { perror("\n socket is not created"); } else perror("\n socket created successfully"); server.sin_port=0; server.sin_addr.s_addr=INADDR_ANY; if(bind(sock,(struct sockaddr *)&server,sizeof(server))<0) { perror("\n error in binding server"); } name_len=sizeof(server); if(getsockname(sock,(struct sockaddr *)&server,(socklen_t *)&name_len)<0) { perror("\n error getting in getsockname()"); } printf("\n the assigned port is %d",htons(server.sin_port)); client_add_size=sizeof(client); printf("\n waiting for message to arrive"); if(recvfrom(sock,buff,sizeof(buff),0,(struct sockaddr *)NULL,NULL)<0) { perror("\n error in recvfrom()" ); } printf("\n data has been received from socket"); printf("\n %s",buff); return 0; } CLIENT PROGRAM #include<stdio.h> #include<stdlib.h> #include<sys/socket.h> #include<netinet/in.h> #include<string.h>

NAME: AMISHA K REG NO: 31010205003

EXPT NO: DATE: #include<arpa/inet.h> int main(int argc, char **argv) { int sock; char string[100],buf[32]; int port; struct sockaddr_in server; port=htons(atoi(argv[2])); printf("\ncreating datagram socket"); sock=socket(AF_INET,SOCK_DGRAM,0); if(sock==-1) perror("\nsocket not created"); else perror("\nsocket created successfully\n"); server.sin_port=port; server.sin_addr.s_addr=inet_addr(argv[1]); printf("\n enter data to be sent"); scanf("%s",string); strcpy(buf,string); printf("\n sending data to the socket\n"); sendto(sock,buf,(strlen(buf)+1),0,(struct sockaddr*)&server,sizeof(server)); printf("\ndata has been sent to the socket"); return 0; } OUTPUT SERVER [beit19@localhost har]$ cc udps.c [beit19@localhost har]$ ./a.out socket created successfully: Success the assigned port is 38726 waiting for message to arrive data has been received from socket hi.. CLIENT [beit03@localhost ~]$ cc udc.c [beit03@localhost ~]$ ./a.out 172.17.1.100 38726 socket created successfully : Success creating datagram socket enter data to be sent hi.. sending data to the socket data has been sent to the socket

NAME: AMISHA K REG NO: 31010205003

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy