hi everybody
i am nwebie in this forum but i think it is useful for me and the member are helpful
my project is about connecting client with the server to start exchanging messages between them.
to be more clear we process this purpose we serve this to the student in the university. how??
student will send a message that contains his name,id and request by format the server want such as zaina-20024008-grade.
the grade is the request and it will be different from one to another becouse it may be subject,pass credit or remain credits an so on. and then click send
when the message recievd to the server the server will superate the message to keep name , id and grade in its location to use it later. then it will access to the database with the name and id it got and start to get the request result and then send it back to the client and ask to the client if he need to get othe info or terminate
i thought to communicate the client with the server by socket connection " stream socket" and i have just finshed the code for one side which is for the server and still working to the client but i have a question occured in my mind and it may stupid question to you
since we diclare the port number in both client and server for listening to connect and declare the ip number in the client which is the local host name, is must the socket connect to network to available access and connect? in another meaning do i need to use modem to be the server connect to the internet becouse since we declare the host name"ip" we need network layer
does it??
i need you help please and sorry for my weakness language but i hope you understand what i want and looking for
i post the code here and note that the double quotation thais for the database i didnt fill still - package test;
-
-
import java.io.IOException;
-
import java.io.ObjectInputStream;
-
import java.io.ObjectOutputStream;
-
-
import java.net.ServerSocket;
-
import java.net.Socket;
-
-
import java.sql.Connection;
-
import java.sql.DriverManager;
-
import java.sql.ResultSet;
-
import java.sql.SQLException;
-
import java.sql.Statement;
-
-
-
public class PcServer extends Thread {
-
// declare unbound server socket
-
private ServerSocket ServS =null ;
-
//declare unconnected client socket
-
private Socket ClientConnection = null;
-
// input stream from client
-
private ObjectInputStream IS;
-
//output stream to client
-
private ObjectOutputStream OS;
-
-
-
//CONSTRUCTOR
-
public PcServer (Socket ClientSocket){
-
ClientConnection = ClientSocket;
-
}
-
-
public PcServer() {}
-
-
-
// set up run server
-
public void runServer(){
-
try //creat server socket
-
{ ServS = new ServerSocket (3131);//3131 is the port number
-
}
-
catch (IOException e) {
-
System.err.println("Could not listen on port:3131");
-
System.exit(-1);
-
}
-
boolean listening = true;
-
while (listening)
-
{ try
-
{ ClientConnection = ServS.accept();//allow server to accept connection from client
-
-
//open input stream from the socket connection to read the data from the client
-
IS=new ObjectInputStream(ClientConnection.getInputStream());
-
-
//open data out stream from the socket connection to write data to the client
-
OS=new ObjectOutputStream (ClientConnection.getOutputStream());
-
-
//start the process(sending and recieving data) from/to client
-
starExchange();
-
}
-
catch (Exception Ex){
-
Ex.printStackTrace();
-
}
-
try // terminate all connection and close stream gate
-
{ OS.close();
-
IS.close();
-
ClientConnection.close();}
-
catch (Exception ex)
-
{ ex.printStackTrace();}
-
-
}
-
}
-
//start the process of the program
-
public void starExchange() throws Exception{
-
String msg =(String)IS.readObject();//read msg coming from client
-
//EON=End Of Name
-
int EON = msg.indexOf('-');
-
//message getting from client = name-id-request
-
// extract the name from the msg
-
String name = msg.substring(0, EON);
-
//extract id from the msg
-
long ID = Long.parseLong(msg.substring(EON+1, EON+9));
-
-
//extract request from the msg
-
String Request = msg.substring(EON+10, msg.length());
-
//JDBC driver name and database URL
-
final String JDBC_DRIVER ="";
-
final String DATABASE_URL="";
-
-
//manage database connection
-
Connection DBconnection = null;
-
Statement DBstatement = null; // query statement
-
-
//connect to the student database and query database
-
try {
-
// load database driver class
-
Class.forName (JDBC_DRIVER);
-
//establish connection to database
-
DBconnection = DriverManager.getConnection (DATABASE_URL,"","");
-
//create statement for quering database
-
DBstatement = DBconnection.createStatement();
-
request(Request,name,ID, DBconnection);
-
}
-
catch (SQLException sqlException){
-
sqlException.printStackTrace();
-
}
-
finally // close DBstatement and DBconnction
-
{
-
try
-
{ DBstatement.close();
-
DBconnection.close();
-
}
-
catch (Exception exception)
-
{ exception.printStackTrace();
-
}
-
}
-
}
-
-
public void request ( String R,String N,long id, Connection connection) throws Exception
-
{
-
//query database
-
Statement st = connection.createStatement();
-
ResultSet resultset = st.executeQuery("SELECT"+R+"FROM Student-Table WHERE ID="
-
+id+"AND NAME="+N);//select the result according to the name and id of student
-
// return the result according to the request
-
OS.writeObject("NAME:"+N+"-ID:"+id+"-"+R+"is"+resultset+"/n Do you need to get othe information?/n" +
-
-
"send (Y/N) with your request if Y");//SEND RESULT OF STUDENT REQUEST
-
String newR=(String) IS.readObject();//READ THE STUDENT RESPOSE
-
-
-
//CHECKING IF CLIENT WANTS TO GET OTHE INFO.
-
if (newR.charAt(0)=='y'||newR.charAt(0)=='Y')
-
{ // extract client request
-
String newReq=newR.substring(2,newR.length());
-
//recursive function...check other request
-
request(newReq,N,id, connection);
-
}
-
-
}
-
}
-
-
-
-
6 2984 Dököll 2,364
Recognized Expert Top Contributor
I think you English is fine...
Why aren't you using an app server name in the place of your local host name?
Not sure what an application server name would have to do with all this.
@Dokoll : Could you please check your PMs and respond appropriately?
Dököll 2,364
Recognized Expert Top Contributor
My bad, you're right r035198x, will read carefully. Here's a bit of info I found on the site, perhaps that'll steer OP in the right direction: Lesson 1: Socket Communications JosAH 11,448
Recognized Expert MVP @Dököll
That online book was written in 1999; it teaches you old Java; I don't think that posting (almost) random Google results here whithout knowing what the subject is all about is a good idea. Please don't do that anymore; there is an up to date link available to the API documentation and the Tutorials in the first article in this group.
kind regards,
Jos (moderator)
hsn 237
New Member
i will respond to what i understood.
don't include a server name in the program, include its ip address, to make sure that it doesn't change later, set the server to a static IP address.
A question, are you trying to create a chatting service? if so, use the Socket class and the ServerSocket class to implement your service.
and read this tutorial, i think it may help. Lesson 1: Socket Communications
kind regards
hsn
JosAH 11,448
Recognized Expert MVP @hsn
Read my reply #5.
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Slash |
last post by:
Hello everyone
I'm looking for seminar topics (and project ideas too) for my final
year in Computer Engineering. I'm considering doing something on
C++/Linux, considering that I love it so much -- I'm still learning it
though.
The topic could be anything computer-related at all, anything new and
interesting perhaps :) Perhaps a research topic.
|
by: Steve Jorgensen |
last post by:
Hi all,
I'm working on a project through a consulting company, and I'm writing some
database code for use in another programmer's project in Excel/VBA. The other
programmer is working through the same consulting company.
I did not initially know this other programmer's experience level, but he
seemed down to earth and friendly. I saw some signs of trouble after having
him try to integrate some of my code, but chalked it up to him...
|
by: Brett Baisley |
last post by:
At school, we do all of our coding in emacs, but I am trying to get the
example apps working at home using Visual C++.net.
In the example, there are 4 .cpp files (canvas.cpp, main.cpp, myDisplay.cpp
and tween.cpp) a header file (canvas.h) and a makefile that compiles it for
linux.
The tween.cpp is the main file, and it only contains inludes statements to
the other files.
|
by: Martin |
last post by:
Hello,
We are in the process of putting together an A class C++ Development
team(ecommerce / billing applications) for a project in the Bay Area.
Reqt. details:
ReqId : Req-3911 - C++ Senior Developer
Primary Skills: C++, SQL
Secondary Skills: XML, Oracle
Description: As a part of the development team for our client's
|
by: xxbabysue123xx |
last post by:
Heres the problem:
Create a class Student with instance data name, studentNumber, class (where class is a String containing one of the following: “Freshman”, “Sophomore”, “Junior”, “Senior”.
Make the class implement the Comparable interface. Include a toString method.
Write a driver program to demonstrate your work.
Instantiate several objects of the Student class.
Call your methods several times to show that class is ordered.
...
| |
by: squishy |
last post by:
I tried to hire programmers to do some stuff, but cannot find reliable,
intelligent C++ Gurus (at Guru or RentACoder at least). So I am stuck
learning C++ and doing the jobs myself.
I would like to streamline my studying to avoid studying anything that would
only serve to slow my progress while not really adding to the advancement of
my goals.
I want to learn to do the following......
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
| |
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |