Help with Socket Programming code

Here is the code in question in C not 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
66
67
68
69
70
// Example socket client

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT 10000

int main(){

	int	clientSocket, ret;
	struct	sockaddr_in serverAddr;
	char	buffer[1024];
	char	dashes[62];

	memset(dashes, '-', 60);
	dashes[60] = '\n';
	dashes[61] = 0;

	clientSocket = socket(AF_INET, SOCK_STREAM, 0);
	if(clientSocket < 0) {
		printf("[-]Error in connection (1).\n");
		exit(1);
	}

	printf("[+]Client Socket is created.\n");
	printf("[+]Type ':exit' to terminate client.\n");

	memset(&serverAddr, '\0', sizeof(serverAddr));
	serverAddr.sin_family = AF_INET;
	serverAddr.sin_port = htons(PORT);
	serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

	ret = connect(clientSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
	if(ret < 0) {
		printf("[-]Error in connection (2).\n");
		exit(1);
	}
	printf("[+]Connected to Server.\n\n");

	while(1) {
		printf("%s", dashes);
		printf("Client Message (':exit' to terminate):  ");
		bzero(buffer, sizeof(buffer));

		fgets(buffer, sizeof(buffer), stdin);
		strtok(buffer,"\n");

		send(clientSocket, buffer, strlen(buffer), 0);

		if (strcmp(buffer, ":exit") == 0) {
			close(clientSocket);
			printf("[-]Disconnected from server.\n");
			break;
		}

		if (recv(clientSocket, buffer, 1224, 0) < 0) {
			printf("[-]Error in receiving data.\n");
		} else {
			printf("\nFrom Server: \t%s\n", buffer);
		}
	}

	return 0;
}


Update/Compile:
Update the current IP address from 127.0.0.1 to 131.216.23.202
Compile and execute.

When prompted, provide a message (i.e., "hello world") to send to the server (which will be echoed back).

Summary:
Provide a short summary of how the program works (i.e., the key steps being taken).

Results:
Send your name as a message to the server.
Capture the result and include in your response.

Addressing:
What is the original IP Address and why might that be used?
What is the IP Address of the server being accessed?
What is the TCP/IP Port number being used?

Socket Programming:
Provide one advantage to using sockets?
Provide a disadvantage to using sockets?

Please help me answer those questions, the IP part is confusing me foremost.
Last edited on
The IP address 127.0.0.1 is actually the local host, i.e. your computer. Changing it to 131.216.23.202 might address another computer. See:

https://www.lifewire.com/network-computer-special-ip-address-818385
It seems you're learning it backwards.

Ideally you'd learn a bit about IP networking, then write some code to use it.

Actually what happens is, we have to deal with code then learn about IP networking. It happened to me and most programmers.

A quick summary is:
Internet Protocol (IP) allows two endpoints (IP addresses) to pass messages to each other.

Other protocols are built on this basic capability. For example, TCP is a client/server protocol that allows streams to data to be sent from one endpoint to the other in a sort of reliable way. The world wide web is built using TCP/IP.

Your program is a TCP/IP client that uses the sockets library. The sockets library was the first library used for programming IP networks, and tries to make network programming similar to using files. When IP programming began to spread, this original reference implementation was copied, modified, re-written ... and remains the most common way to write networking code for the internet.

A TCP client must:
- create a socket - socket()
- connect to a server at a specified address - connect()

It can then read and write using read/write on Posix systems, or more generally send/recv everywhere.

When you're done you should:
- shutdown writes - tells the server not to expect anything further from you
- close the socket - close()

That's enough to help you understand related reading material and answer some of those questions.
Registered users can post here. Sign in or register to post.