Hello, I am Reza and I have just started programming with C and C++.
I am so interested in Network programming. I would like to know more about sockets and the concepts of it in C and CPP.
Thank you in advance.

Recommended Answers

All 2 Replies

Post your questions in the C and/or C++ forums.

Use Java. JVM is one of the better networking compilers plus, there are no external libraries needed.
Here is a client program

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;


public class client {
    public static void main(String args[]) {
        int portNum = 8080;
        String hostNam = "192.168.11.4";
        try {
            System.out.println("Connecting to server...");
            Socket sock = new Socket(hostNam, portNum);
            PrintWriter out = new PrintWriter(sock.getOutputStream());

            BufferedReader stdIn = new BufferedReader(  new InputStreamReader(System.in));
            System.out.println("Success, we're in!");
            String input;
            while((input = stdIn.readLine()) != null) {
                out.println(input);
                out.flush();
                out.flush();
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Here is the server

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;


public class server {
    public static void main(String[] args) {
        int portNum = 8080;
        try {
            System.out.println("Starting server...");
            ServerSocket ss = new ServerSocket(portNum);
            System.out.println("Server started...");
            Socket client = ss.accept();
            System.out.println("Client accepted...");
            PrintWriter out = new PrintWriter(client.getOutputStream());
            BufferedReader bff = new BufferedReader(new InputStreamReader(client.getInputStream()));

            String input;
            while((input = bff.readLine()) != null) {
                System.out.println("Message recived.." + input);
                out.println(input);
                out.flush();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

You may also use Javascript. Here is a simple program that tracks if soneone has connected to an ip

var app = require('express')();
var http = require('http').Server(app);
var net = require('socket.io')(http);
var views = 0;

app.get('/', function(req, res) {
    res.sendFile(__dirname + '/chat.html');
});

http.listen(3000, function() {
    console.log("Loaded!");
});
net.on('connection', function() {
    console.log("A user has connected!");
});

Here is the HTML

<html>
  <head>
    <title>Views</title>
  </head>
  <style>
  #msgs {list-style-type: none; background: #99CCFF}
  </style>
  <body>
    <center>
      <ul id="msgs">Websites!</ul>
    </center>
  </body>
</html>

When running this, you must install Node.js, go to your directory where your file is saved, then in command, type npm install -- save express.js, then npm install --save socket.io, then type node FILENAME.js

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.