Hot questions for Using Transmission Control Protocol in linux

Question:

Is a there a way to block messages on a port in linux (or Programmatically)?.

Say I have a server 'S' and a client 'C'. If the client is sending messages on post 12345, a linux command yield below like this:

 lsof -i | grep 12345

 java     52148 john.doe   26w  IPv6 1041861063      0t0  TCP  localhost:53413->localhost:12345 (ESTABLISHED)
 java     52164 john.doe    5u  IPv6 1041861064      0t0  TCP localhost:11400->localhost:12345 (ESTABLISHED)
 java     52164 john.doe   46r  IPv6 1041860804      0t0  TCP localhost:12345 (LISTEN)

Is there a way to block the client from sending messages to the server? Or is there a way to block the port(12345) that client is sending messages through to the server?


Answer:

Controlling access to TCP/IP ports is the responsibility of your Firewall. This is done with iptables under Linux.

For example to stop incoming connections to your server, use the following command on the machine that is running the server.

/sbin/iptables -A INPUT -p tcp --destination-port 12345 -j DROP

To stop outgoing connections from the server to the client use

/sbin/iptables -A OUTPUT -p tcp --destination-port 12345 -j DROP

To do this on a per IP basis use

/sbin/iptables -A OUTPUT -p tcp -d {ip_address} --dport 12345 -j DROP

oh and don't forget to

/sbin/service iptables save

when you're done.

Question:

I have to open plenty of TCP connections to a SIP server which is runnning on linux. I tried with one simple client program in Java, but I could not open even 350 connections from another linux server. I want to open ~ 50 thousand and above for a load/performance test.

Is there any way to overcome this? What are the limitations? Sorry if this is a silly question,I am a Beginner.

Thanks

client program

public class ConnectionTcp
{
static int noOfconnected;
Socket socket;
static int port=1000;
static Object obj=new Object();
static AtomicInteger atomicInteger;
public static void main(String[]args)
{
try{
ConnectionTcp con=new ConnectionTcp();
atomicInteger = new AtomicInteger();
Date date = new Date();
for(int i=0;i<50000;i++)
{
port+=i;    

con.sendmsg();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public  synchronized  void sendmsg(){
        try{
        Thread.sleep(100);  
        }
        catch(Exception e){
        System.out.println(e);
        }    
        Runnable r=new Runnable(){
            public void run(){
                try{
                    boolean check=true;
                    InetAddress ip=InetAddress.getByName("131.10.20.16");  
                    Socket socket=new Socket("131.10.20.17",5060,ip,port);              
                    System.out.println("conected is "+socket.isConnected()+"<----------with port----------->"+socket.getLocalPort());


                    OutputStream out =socket.getOutputStream();
                    InputStream in =socket.getInputStream();
                        String str = "keep alive";
                        byte[] array = str.getBytes();          
                    System.out.println("no of user connected with server is "+atomicInteger.incrementAndGet());
                    while(true){                    
                        try{
                            int i = in.read();
                            out.write(array);                           
                        }catch(Exception e){
                            System.out.println("exception"+e);
                            atomicInteger.decrementAndGet();
                            socket.close();
                            Date date = new Date();    
                             System.out.println("Ented Time  is "+date.toString());
                            break;
                        }
                    }
                }catch(Exception e1){   
                System.out.println("main exception"+e1);
                atomicInteger.decrementAndGet();
                }
            }
        };
        (new Thread(r,"tcp")).start();        
    }
}

Answer:

You can only use ports above 1023. The lower numbers are reserved.

Question:

I try to connect to a mySql localhost database. I checked the port 1487 and is open for connection. Many solutions suggest to open SQL Server Configuration Manager, but there is no such thing in Ubuntu 14.04. I tried to install Microsoft ODBC Driver 11 for SQL Server on Linux, but it's only for 64 bit systems. I use eclipse to write my java code:

    String dbURL = "jdbc:sqlserver://localhost:1487;user=sa;password=1234";
    conn = DriverManager.getConnection(dbURL);
    if (conn != null) {
             System.out.println("Connected");
    }

Error:

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host SQL2008, port 1487 has failed. Error: "null. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.

Answer:

SQL Server is a database produced by Microsoft based on a Sybase database. I believe these have compatible SQL connectors. Note: I don't believe SQL Server runs on Linux.

However, MySQL is a completely different database. As such I suggest using the MySQL JDBC drivers, or the MariaDB Connector/J and URL to connect to MySQL as this is more likely to work.

See https://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-connect-drivermanager.html

The example it gives is

conn =
   DriverManager.getConnection("jdbc:mysql://localhost/test?" +
                               "user=minty&password=greatsqldb");

Note: how this mentions mysql not sqlserver