Posted: . At: 9:15 AM. This was 1 year ago. Post ID: 17439
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.



Sponsored



A simple netcat program in C to receive data and then acknowledge this with a message.


netcat.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
 
/********************************************************************
* Description:
* Author: jason,,, <>
* Created at: Fri Jan 13 08:30:53 AEDT 2023
* Computer: jason-Lenovo-H50-55
* System: Linux 5.15.0-57-generic on x86_64
*
* Copyright (c) 2023 jason,,,  All rights reserved.
*
********************************************************************/
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
 
void error(const char *msg) {
    perror(msg);
    exit(1);
}
 
int main(int argc, char *argv[]) {
    int sockfd, newsockfd, portno;
    socklen_t clilen;
    char buffer[256];
    struct sockaddr_in serv_addr, cli_addr;
    int n;
 
    // create a socket
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
        error("ERROR opening socket");
 
    // clear address structure
    bzero((char *) &serv_addr, sizeof(serv_addr));
 
    // set port number
    portno = 5080;
 
    // setup the host_addr structure for use in bind call
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);
 
    // bind the socket to the specified port
    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) 
        error("ERROR on binding");
 
    // listen for clients
    listen(sockfd, 5);
    clilen = sizeof(cli_addr);
 
    // accept a client
    newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
    if (newsockfd < 0) 
        error("ERROR on accept");
 
    // read data from client
    bzero(buffer, 256);
    n = read(newsockfd, buffer, 255);
    if (n < 0) 
        error("ERROR reading from socket");
    printf("Here is the message: %s\n", buffer);
 
    // write data to client
    n = write(newsockfd, "I got your message", 18);
    if (n < 0) 
        error("ERROR writing to socket");
 
    // close sockets
    close(newsockfd);
    close(sockfd);
 
    return 0;
}

This is a very useful little program. This opens port 5080 and listens for a netcat connection.

Then it will print a text message when a text string is sent to it. As shown below, I connected to the remote netcat server and then sent “Hello.” as a message and it was acknowledged automatically.

┌──(john㉿DESKTOP-PF01IEE)-[~]
└─$ netcat 192.168.1.2 5080
Hello.
I got your message

This works perfectly.

Scanning the system with Nmap, I can see the open port on 5080. Once the connection is established and the message is sent, the program closes. But this could be used for a variety of things.

┌──(john㉿DESKTOP-PF01IEE)-[~]
└─$ nmap -p 3000-6000 192.168.1.2
Starting Nmap 7.93 ( https://nmap.org ) at 2023-01-13 08:51 AEDT
Nmap scan report for 192.168.1.2
Host is up (0.85s latency).
Not shown: 2999 closed tcp ports (conn-refused)
PORT     STATE SERVICE
3389/tcp open  ms-wbt-server
5080/tcp open  onscreen
 
Nmap done: 1 IP address (1 host up) scanned in 2.14 seconds

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.