photo of Angga Lingga

ALin's Blog

Cinta merupakan kunci untuk hidup baik, bukan agama atau hukum

Subscribe to RSS feed

Chatting Using Java

Main Server
// File : ServerChat.java
// Created by : Angga Lingga

import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;

public class ServerChat
{
	public static Map userList;
	
    public static void main(String[] args) throws IOException
    {
		userList = new HashMap<String, Socket>();
        ServerSocket serverChatSocket = null;
        boolean listening = true;

        try {
            serverChatSocket = new ServerSocket(4417);
        } catch (IOException e) {
        	JOptionPane.showMessageDialog(null, "Could not listen on port: 4417.", "ERROR", JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }

        while (listening)
        {
	    	new ServerThread(serverChatSocket.accept()).start();
			userList.put(serverChatSocket.getInetAddress(), serverChatSocket);
			System.out.println("Server Chat : " + userList.size());
        }

        serverChatSocket.close();
    }
}


Server Thread
// File : ServerThread.java
// Created by : Angga Lingga
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.util.*;

public class ServerThread extends Thread
{
    private Socket serverSocket = null;
	private ObjectInputStream input;
	private ObjectOutputStream output;
	private final static String SEPARATOR = "\\*\\*\\*";
	private final static String PUBLIC = "public";
	private final static String ONLINE_USER = " has joined the room";
	private String[] fromUser;
	private String message;
	
    public ServerThread(Socket socket) 
    {
		super("ServerThread");
		serverSocket = socket;
		message = "";
    }

    public void run() 
    {
		try 
		{
			System.out.println("Starting Session");
			while(true)
			{
				try
				{
					output = new ObjectOutputStream(serverSocket.getOutputStream());
					output.flush();
					input = new ObjectInputStream(serverSocket.getInputStream());
					
					processConnection();
					break;
				}
				catch(EOFException eofe)
				{
					System.out.println("An Error has Occured, please check it");
				}
				finally
				{
					input.close();
					output.close();
					serverSocket.close();
				}
			}
			System.out.println("Ending Session");
		}
		catch (IOException e) 
		{
		    e.printStackTrace();
		}
    }
    
	public void processConnection()
	{
		do
		{
			try
			{
				message = (String) input.readObject();
				fromUser = message.split(SEPARATOR);
				System.out.println("Message from : " + serverSocket.getInetAddress() + " " + fromUser[2]);
				message = PUBLIC + "*" + serverSocket.getInetAddress() + "*" + fromUser[2];
				System.out.println(ServerChat.userList.size());
    			
				sendAllMessageToAllUser(message);
				System.out.println("Message to : " + serverSocket.getInetAddress() + " " + message);
				System.out.println(fromUser.length);
			}
			catch(ClassNotFoundException cnfe)
			{
				System.out.println("Unknown object type received");
			}
			catch(IOException ioe)
			{
				System.out.println("An Error has Occured, please check it");
				break;
			}
		}
		while(!message.equals("CLIENT >> TERMINATED_APPLICATION"));
	}
	
	public void sendAllMessageToAllUser(String Message)
	{
		try
		{
			output.writeObject(Message);
			output.flush();
		}
		catch(IOException ioe)
		{
			System.out.println("An Error has Occured, please check it");
		}
	}
}

If you want to use the server please use this command:
- javac *.java
- java ServerChat

Show File using Java

import java.io.*;  
    class ShowFile {  
		public static void main (String[] args) throws IOException {  
			  int i;  
			  FileInputStream fin;  
			  try {  
					   fin = new FileInputStream(args[0]);  
			 } catch (FileNotFoundException e) {  
					  System.out.println("File not found.");  
					 return;  
			 } catch (ArrayIndexOutOfBoundsException e) {  
					 System.out.println("Usage: Showfile file");  
					 return;   
			 }  
		   do {  
				   i = fin.read();  
				 if (i!= -1) System.out.print((char) i);  
		  } while (i!=-1);  
		  fin.close();  
       }  
}

Copy File using Java

import java.io.*;

class CopyFile {  
  public static void main(String args[]) throws IOException   {  
    int i;  
    FileInputStream fin; FileOutputStream fout;  
    try {  
		try {  
			fin = new FileInputStream(args[0]);  
		}  
		catch (FileNotFoundException e) {  
			System.out.println("Input file not found.");  
			return;  
		}  
		try {  
			fout = new FileOutputStream(args[2]);  
		} 
		catch (FileNotFoundException e) {  
          System.out.println("Error opening output file.");  
          return;  
		}  
	}catch (IndexOutOfBoundsException e) {  
       System.out.println("Usage: CopyFile from .. to ..");  
      return;   
    }  
 try {  
    do {  
       i = fin.read();  
       if (i!=-1)               
          fout.write(i);  
    } while (i!= -1);  
 }  
 catch (IOException e) {  
    System.out.println("File error.");  
 }  
 
 fin.close();  
 fout.close();   
}  
}

Calculator Java

Ah............

Mengapa harus ada sih pelajaran dan mengapa harus ada Kuliah???

Read and Write File in Visual Basic 6.0

Reading File
Option Explicit

Dim fso As FileSystemObject
Dim fileMhs As File
Dim txtsMhs As TextStream

Private Sub Form_Load()
     Set fso = New FileSystemObject
     Set fileMhs = fso.GetFile("c:\mahasiswa.txt")
     Set txtsMhs = fileMhs.OpenAsTextStream(ForReading)
End Sub

Private Sub cmdBaca_Click()
     Dim mhs As String
     Dim dataMhs() As String

     On Error GoTo errHandler
     mhs = txtsMhs.ReadLine

     dataMhs = Split(mhs, "#")

     txtNIM.Text = dataMhs(0)
     txtNama.Text = dataMhs(1)
     txtAlamat.Text = dataMhs(2)

     Exit Sub

errHandler:
     If Err.Number = 62 Then
          MsgBox "This file has no contents", vbCritical, "Error"
          txtsMhs.Close
     Else
          MsgBox "Error: " & Err.Description, vbCritical, "Error"
     End If
End Sub


Writing File
Option Explicit

Dim fso As FileSystemObject
Dim fileMhs As file
Dim txtsMhs As TextStream

Private Sub Form_Load()
     Set fso = New FileSystemObject
     fso.CreateTextFile ("c:\mahasiswa.txt")
     Set fileMhs = fso.GetFile("c:\mahasiswa.txt")
     Set txtsMhs = fileMhs.OpenAsTextStream(ForWriting)
End Sub

Private Sub cmdSimpan_Click()
     txtsMhs.Write (txtNIM.Text & "#" & txtNama.Text & "#" & txtAlamat.Text)
End Sub
February 2012
M T W T F S S
January 2012March 2012
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