-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.c
65 lines (52 loc) · 1.7 KB
/
Client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* This file handles the client side input in client-server sockets.
*
* Author Tim Solokha
* Version 1.0
* File: Client.c
* Created: Feb 2020
* ©Copyright Tim Solokha. All rights reserved.
*
* Description: This class uses socket libraries to connect to a server. It allows
* for user connection to the port 15684, and lets the user use command line arguments.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include<string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 15684
int main(int argc,char **argv)
{
int sockfd,n;
char sendline[100];
char recvline[100];
struct sockaddr_in servaddr;
if(argc == 1) {
// Need at least two command line arguments to run program
printf("Not enough commands, try again.\n");
}
if(argc == 2) {
// Creates the inital socket for connection
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof servaddr);
// Enbales connection to user specified port
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(PORT);
// Delcaration of the IP address
inet_pton(AF_INET,"127.0.0.1",&(servaddr.sin_addr));
// Connect port number to the IP address
connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
}
// Clears any previous string arguments
bzero( recvline, 100);
// Writes, reads, and displays the given string
write(sockfd,argv[1],strlen(argv[1])+1);
read(sockfd,recvline,100);
printf("Usage: Modify user inputted string.\n");
printf("%s\n",recvline);
// Closes socket in order to prevent broken pipes
close(sockfd);
}