JAva Swing/SQL Server 2000 sending messages in a LAN

Rajnesh 0 Tallied Votes 206 Views Share

This prog uses SQL Server's Master Databases xp_cmdshell stored procedure (Shell Commands) to send messages in a LAN.Make Sure the driver name is 'netsend' and it points to Master Database. Check and change the username and passwords in the Java Program also.Make sure you have sufficient priviledges to execute stored procedures in SQL Server 2000.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.util.*;

public class NetSend extends JFrame implements ActionListener
{
	JComboBox list = new JComboBox();
	JTextField message = new JTextField(20);
	JButton send = new JButton("Send");
	JTextField result = new JTextField(25);
	
	private static Connection cn = null;						
 	private static Statement st= null;
	private ResultSet r= null;

	public NetSend(String title){
		setTitle(title);
		initialise();
		Container c = getContentPane();
		c.setLayout(new FlowLayout(FlowLayout.CENTER));
		c.add(list);
		c.add(message);
		send.addActionListener(this);
		send.setMnemonic(KeyEvent.VK_S);
		c.add(send);
		result.setEditable(false);
		result.setBackground(Color.yellow);
		result.setForeground(Color.red);
		c.add(result);
	}
	public void actionPerformed(ActionEvent ae){
		if(ae.getSource()==send){
			String dest = String.valueOf(list.getSelectedItem());
			String msg = message.getText();
			try{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			cn = DriverManager.getConnection("jdbc:odbc:netsend","mcp","mcp");
			st=cn.createStatement();
			ResultSet r=st.executeQuery("xp_cmdshell 'net send "+dest+" "+msg+"'");
			result.setText("Message Sent");
			
			}catch(Exception e){result.setText("Error in Sending Message");}
		}
	}
	public void initialise(){
		try
		{	
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		
			cn = DriverManager.getConnection("jdbc:odbc:netsend","mcp","mcp");
			st=cn.createStatement();
			r=st.executeQuery("xp_cmdshell 'net view'");			
			
			String temp = new String();
			int i = 1;
			ArrayList machNames = new ArrayList();
			while(r.next())
			{
				machNames.add(r.getString("output"));
			}
			for(int x=3;x<machNames.size()-3;x++){
				temp = String.valueOf(machNames.get(x));				
				list.addItem(temp.substring(2,temp.length()));				
			}	
			result.setText("Type your message and press ALT and S");
		}
		catch(Exception e){result.setText("Not able to retrieve the hosts");}	
	}
	public static void main(String args[])
	{
		NetSend ns = new NetSend("Send your messages to PC's in LAN");
		ns.setVisible(true);
		ns.setSize(400,400);
	}		
}

Dani AI

Generated

Clarification for and follow-up to : the label "netsend" in the code is just an ODBC data source name (DSN) — an alias you configure in the Windows ODBC Administrator. The program opens that DSN via the JDBC-ODBC bridge and asks the database server to execute operating-system commands on the server side, which is why the host list and the messaging are driven by server-side network commands instead of pure Java networking.

Important cautions: asking the database to run OS commands requires high privileges and creates a major attack surface (SQL injection or an elevated DB account can lead to arbitrary server code execution). The Windows messaging mechanism the code relies on is tied to legacy services that are not present or enabled on many modern Windows systems. For reliability and security, avoid shelling out from the DB whenever possible.

Safer, simpler alternative: send messages directly from Java over the LAN. A tiny UDP example (sender + listener) shows the idea:

import java.net.*;
import java.nio.charset.StandardCharsets;

public class UdpSender {
  public static void send(String host, int port, String text) throws Exception {
    DatagramSocket sock = new DatagramSocket();
    byte[] data = text.getBytes(StandardCharsets.UTF_8);
    DatagramPacket pkt = new DatagramPacket(data, data.length, InetAddress.getByName(host), port);
    sock.send(pkt);
    sock.close();
  }
}
import java.net.*;
import java.nio.charset.StandardCharsets;

public class UdpListener {
  public static void listen(int port) throws Exception {
    DatagramSocket sock = new DatagramSocket(port);
    byte[] buf = new byte[4096];
    while (true) {
      DatagramPacket pkt = new DatagramPacket(buf, buf.length);
      sock.receive(pkt);
      String msg = new String(pkt.getData(), 0, pkt.getLength(), StandardCharsets.UTF_8);
      System.out.printf("From %s: %s%n", pkt.getAddress().getHostAddress(), msg);
    }
  }
}

Notes: choose a non-blocked port and account for firewalls; broadcast vs. unicast behavior differs by network. If a DB-centric solution is mandatory, use a proper vendor JDBC driver (not the JDBC-ODBC bridge), restrict the DB account to minimum rights, avoid executing raw shell commands, and consider safer patterns (a message table poller, DB Service Broker, or a dedicated messaging broker) instead of server-side shell execution.

Antony Prabu 0 Newbie Poster

I have a doubt in database connectivity. Could you please tell me how it's work and what does it mean netsend driver.

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.