removed additional methods so that the code reads linearly in server-tcp and client-tcp
parent
6c518455dd
commit
ae1ea7d33d
|
@ -37,31 +37,12 @@
|
||||||
* this function will send the inputted string to the server and then
|
* this function will send the inputted string to the server and then
|
||||||
* recieve the string from the server outputing it to the termial
|
* recieve the string from the server outputing it to the termial
|
||||||
*/
|
*/
|
||||||
int SendReceive(int sockfd)
|
|
||||||
{
|
|
||||||
char sendline[MAXLINE]="Hello Server"; /* string to send to the server */
|
|
||||||
char recvline[MAXLINE]; /* string received from the server */
|
|
||||||
|
|
||||||
/* write string to the server */
|
|
||||||
if (write(sockfd, sendline, strlen(sendline)) != strlen(sendline)) {
|
|
||||||
printf("Write Error to Server\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* flags if the server stopped before the client could end */
|
|
||||||
if (read(sockfd, recvline, MAXLINE) == 0) {
|
|
||||||
printf("Client: Server Terminated Prematurely!\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("Server Message: %s\n", recvline);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int sockfd, ret;
|
int sockfd, ret;
|
||||||
|
char sendline[MAXLINE]="Hello Server"; /* string to send to the server */
|
||||||
|
char recvline[MAXLINE]; /* string received from the server */
|
||||||
struct sockaddr_in servaddr;
|
struct sockaddr_in servaddr;
|
||||||
|
|
||||||
/* must include an ip address or this will flag */
|
/* must include an ip address or this will flag */
|
||||||
|
@ -96,11 +77,20 @@ int main(int argc, char **argv)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* takes inputting string and outputs it to the server */
|
/* takes inputting string and outputs it to the server */
|
||||||
ret = SendReceive(sockfd);
|
/* write string to the server */
|
||||||
if (ret != 0){
|
if (write(sockfd, sendline, strlen(sendline)) != strlen(sendline)) {
|
||||||
printf("Send Recieve Error");
|
printf("Write Error to Server\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* flags if the server stopped before the client could end */
|
||||||
|
if (read(sockfd, recvline, MAXLINE) == 0) {
|
||||||
|
printf("Client: Server Terminated Prematurely!\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Server Message: %s\n", recvline);
|
||||||
|
|
||||||
/* close socket and connection */
|
/* close socket and connection */
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
|
|
||||||
|
|
|
@ -42,34 +42,17 @@ void err_sys(const char *err, ...)
|
||||||
printf("Fatal error : %s\n", err);
|
printf("Fatal error : %s\n", err);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Handles response to client.
|
|
||||||
*/
|
|
||||||
void respond(int sockfd)
|
|
||||||
{
|
|
||||||
int n; /* length of string read */
|
|
||||||
char buf[MAXLINE]; /* string read from client */
|
|
||||||
char response[22] = "I hear ya for shizzle";
|
|
||||||
memset(buf, 0, MAXLINE);
|
|
||||||
n = read(sockfd, buf, MAXLINE);
|
|
||||||
if (n > 0) {
|
|
||||||
printf("%s\n", buf);
|
|
||||||
if (write(sockfd, response, 22) > 22) {
|
|
||||||
err_sys("write error");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (n < 0) {
|
|
||||||
err_sys("respond: read error");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int listenfd, connfd;
|
int listenfd, connfd;
|
||||||
int opt;
|
int opt;
|
||||||
|
int n; /* length of string read */
|
||||||
|
char buff[MAXLINE];
|
||||||
|
char buf[MAXLINE]; /* string read from client */
|
||||||
|
char response[22] = "I hear ya for shizzle";
|
||||||
struct sockaddr_in cliAddr, servAddr;
|
struct sockaddr_in cliAddr, servAddr;
|
||||||
char buff[MAXLINE];
|
|
||||||
socklen_t cliLen;
|
socklen_t cliLen;
|
||||||
|
|
||||||
/* find a socket , 0 for using TCP option */
|
/* find a socket , 0 for using TCP option */
|
||||||
listenfd = socket(AF_INET, SOCK_STREAM, 0);
|
listenfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
@ -109,8 +92,19 @@ int main()
|
||||||
printf("Connection from %s, port %d\n",
|
printf("Connection from %s, port %d\n",
|
||||||
inet_ntop(AF_INET, &cliAddr.sin_addr, buff, sizeof(buff)),
|
inet_ntop(AF_INET, &cliAddr.sin_addr, buff, sizeof(buff)),
|
||||||
ntohs(cliAddr.sin_port));
|
ntohs(cliAddr.sin_port));
|
||||||
|
|
||||||
respond(connfd);
|
/* empty response buffer to avoid unexpected output */
|
||||||
|
memset(buf, 0, MAXLINE);
|
||||||
|
n = read(connfd, buf, MAXLINE);
|
||||||
|
if (n > 0) {
|
||||||
|
printf("%s\n", buf);
|
||||||
|
if (write(connfd, response, 22) > 22) {
|
||||||
|
err_sys("write error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (n < 0) {
|
||||||
|
err_sys("respond: read error");
|
||||||
|
}
|
||||||
/* closes the connections after responding */
|
/* closes the connections after responding */
|
||||||
if (close(connfd) == -1) {
|
if (close(connfd) == -1) {
|
||||||
err_sys("close error");
|
err_sys("close error");
|
||||||
|
|
Loading…
Reference in New Issue