-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsocket.c
More file actions
executable file
·131 lines (101 loc) · 2.53 KB
/
Copy pathsocket.c
File metadata and controls
executable file
·131 lines (101 loc) · 2.53 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
*Subject : COMP30017 Operating System and Network Services
*Project : Project 2
*FileName : main.c
*Author : Tofi Buzali 513816 & Lucy Munanto 346003
*/
#include "socket.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
/*
* Creates a new socket and returns the socket descriptor.
*/
int socket_setup(int portno){
int sockfd;
struct sockaddr_in serv_addr;
//If Port Number isn't specified
if (portno == (int)NULL){
fprintf(stderr, "ERROR, no port provided\n");
exit(1);
}
//Create a new socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0){
perror("ERROR opening socket");
exit(1);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
//Bind the socket to a Port Number
if (bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
{
perror("ERROR on binding");
exit(1);
}
return sockfd;
}
/*
* Listens for a new connection,
* once there is a new connection
* the file descriptor of the new open connection is returned.
*/
int socket_get_new_connection(int sockfd){
int newSockfd;
int clilen;
struct sockaddr_in cli_addr;
//Listen for a new connection
listen(sockfd, 100);
clilen = sizeof(cli_addr);
/*Accept a connection - block until a connection is ready to be
* accepted. newSockfd is the new file descriptor to communicate on*/
newSockfd = accept(sockfd, (struct sockaddr*)&cli_addr, (unsigned int*)&clilen);
if (newSockfd < 0){
perror ("ERROR on accept");
exit(1);
}
return newSockfd;
}
/*
* Reads a 'line' from the given connection.
* A 'line' means a sequence of characters ending with a CRLF pair.
* The string being returned must include the CRLF pair.
*/
char* socket_get_line(int fd){
int n;
char *buffer = malloc(256);
n = (int)read (fd, buffer, 255);
if (n < 0) {
perror("ERROR reading from socket");
exit(1);
}
return buffer;
}
/*
* Writes the given string to the given connection descriptor.
*/
void socket_write(int fd, char* string){
int n;
n = (int) write (fd, string, strlen(string));
if (n < 0){
perror("ERROR writing to socket");
exit(1);
}
}
/*
* Closes the given connection descriptor.
*/
void close_connection(int fd){
close(fd);
}
/*
* Closes the given socket.
*/
void close_socket(int fd){
close(fd);
}