Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Nama : Raviqa Sandra Putri
- // Nim : 1856401009
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #define MAXBUF 1024
- int main(int argc, char* argv[]){
- int udpSocket;
- int returnStatus, returnStatus1;
- int addrlen;
- struct sockaddr_in udpClient, udpServer;
- char buf[MAXBUF];
- char buf1[MAXBUF];
- if(argc < 5){
- fprintf(stderr, "Usage: %s <ip address> <port> <username> <password>\n", argv[0]);
- exit(1);
- }
- udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
- if(udpSocket == -1){
- fprintf(stderr, "Could not create a scoket!\n");
- exit(1);
- } else {
- printf("Socket created.\n");
- }
- udpClient.sin_family = AF_INET;
- udpClient.sin_addr.s_addr = INADDR_ANY;
- udpClient.sin_port = 0;
- returnStatus = bind(udpSocket, (struct sockaddr*)&udpClient, sizeof(udpClient));
- if(returnStatus == 0 ){
- fprintf(stderr, "Bind Completed!\n");
- } else {
- fprintf(stderr, "Could not bind to address!\n");
- close(udpSocket);
- exit(1);
- }
- udpServer.sin_family = AF_INET;
- udpServer.sin_addr.s_addr = inet_addr(argv[1]);
- udpServer.sin_port = htons(atoi(argv[2]));
- strcpy(buf, argv[3]);
- returnStatus = sendto(udpSocket, buf, strlen(buf)+1, 0, (struct sockaddr*)&udpServer, sizeof(udpServer));
- strcpy(buf1, argv[4]);
- returnStatus = sendto(udpSocket, buf1, strlen(buf)+1, 0, (struct sockaddr*)&udpServer, sizeof(udpServer));
- if(returnStatus == -1 && returnStatus == -1){
- fprintf(stderr, "Could not send message!\n");
- } else {
- printf("Message sent.\n");
- /*message sent look for confirmation*/
- addrlen = sizeof(udpServer);
- returnStatus = recvfrom(udpSocket, buf, MAXBUF, 0, (struct sockaddr*)&udpServer, &addrlen);
- if (returnStatus == -1){
- fprintf(stderr, "Did not Receive confirmation!\n");
- }
- else {
- buf[returnStatus] = 0;
- printf("received: %s\n",buf);
- }
- }
- close(udpSocket);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement