|
it is implemented on the basis of the code
package socket_tcp;
import java.io.*;
import java.net.*;
public class jTcpCliOnly {
public static void main(String[] args) throws IOException {
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
String hostName = new String("hp-HP"); //Your: [XXXXXX] to get
try {
echoSocket = new Socket(hostName, 7000);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + hostName);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for " + "the connection to: " + hostName);
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput); //write what user input to TCP server
System.out.println("echo: " + in.readLine());
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
___________________________________________________________-
package socket_tcp;
import java.io.*;
import java.net.*;
public class jTcpSerOnly {
public static void main(String[] args) throws IOException {
boolean flag = true;
ServerSocket serverSocket = null;
Socket clientSocket = null;
String inputLine;
BufferedReader is = null;
PrintWriter os = null;
try {
serverSocket = new ServerSocket(7000);
System.out.println("Server listen on: " + serverSocket.getLocalPort());
while (flag) {
clientSocket = serverSocket.accept();
is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
os = new PrintWriter(clientSocket.getOutputStream(), true);
while ((inputLine = is.readLine()) != null) {
if (inputLine.equals("Stop!")) {
flag = false;
break;
}
System.out.println("Server: " + inputLine);
os.println(inputLine);
os.flush();
}
os.close();
is.close();
clientSocket.close();
}
} catch (IOException e) {
System.err.println("Exception: " + e);
}
}
}
|
|
|
|
|
i just want to use java socket implementation sends a c struct
|
|
|
|
|
You just need to send it as a stream of bytes, the same as any other data. Where is this structure coming from?
Veni, vidi, abiit domum
|
|
|
|
|
This structure needs to define with java
|
|
|
|
|
Sorry, but I do not understand what you mean. Java does not have structs, only classes.
Veni, vidi, abiit domum
|
|
|
|
|
rualchina wrote:
i just want to use java socket implementation
sends a c struct
Sockets do not send structures. Not in java, not in C, not in C++.
What they send is data. Nothing else. It up to the two ends to decide what the data represents - not the socket.
Your struct is just 4 integers of which you have not explicitly defined the following
- Size of each int.
- Endianess of the int.
After you know that then you do what I already said.
|
|
|
|
|
|
I am amazed how many times this question has come up recently: an image file is just a file, a stream of bytes, the same as all other files. Transferring it from one place to another is exactly the same: read some bytes, send them to the receiver, repeat until all bytes have been transferred.
Member 10328278 wrote: P.S.I eed full code Sorry, but that's your job, we don't provide code to order.
Veni, vidi, abiit domum
|
|
|
|
|
Upvoted... this has come up a lot, and not sure why someone would downvote since you still did answer the question.
|
|
|
|
|
Thanks.
Albert Holguin wrote: not sure why someone would downvote Probably because I did not provide the code for him.
Veni, vidi, abiit domum
|
|
|
|
|
You need to use an input stream to read the bytes from the image file, and send that through a DataOutputStream/equivalent over the network, to your desired host.
Once the host receives the bytes, save to file with appropriate file extension.
|
|
|
|
|
I want to add two Jpanel on my tabbed pane ,one should be in south with buttons,and other should be in north where I have Text fields and Labels.
My problem is that the buttons on container Jpanel is not showing in south.
Here is the code:
public class Mainframe extends JFrame
{
private static final long serialVersionUID = 1L;
JTabbedPane tabbedPane = new JTabbedPane();
JPanel jp1 = new JPanel();
JPanel container = new JPanel();
JFrame frame = new JFrame("TEST");
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");
JButton button4 = new JButton("Button 4");
JButton button5 = new JButton("Button 5");
private Mainframe()
{
Container contentPane = getContentPane();
container.setLayout(new FlowLayout());
BufferedImage myPicture = null;
try
{
myPicture = ImageIO.read(new File("FINAL.jpg"));
}
catch (IOException e1)
{
e1.printStackTrace();
}
JLabel lbl = new JLabel(new ImageIcon(myPicture));
container.add(lbl);
container.add(button1);
container.add(button2);
container.add(button3);
container.add(button4);
container.add(button5);
jp1.add(container,BorderLayout.SOUTH);
frame.add(tabbedPane);
tabbedPane.addTab("First Tab",jp1);
frame.setPreferredSize(new Dimension(1500, 1210));
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setResizable(true);
frame.setVisible(true);
}
}
|
|
|
|
|
It looks as though you have too many containers. Try simplifying your setup by adding one element at a time to see where it's going wrong. Start with the tab, then add a panel to the tab, then a single item to the panel etc.
Veni, vidi, abiit domum
|
|
|
|
|
Here is how I see it can be simplified ,and still the problem exists.
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
public class Mainframe extends JFrame
{
private static final long serialVersionUID = 1L;
JTabbedPane tabbedPane = new JTabbedPane();
JPanel jp1 = new JPanel();
JPanel Firstpanel = new JPanel();
static JFrame frame = new JFrame("Create Company");
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");
JButton button4 = new JButton("Button 4");
JButton button5 = new JButton("Button 5");
private Mainframe()
{
BufferedImage myPicture = null;
try
{
myPicture = ImageIO.read(new File("FINAL.jpg"));
}
catch (IOException e1)
{
e1.printStackTrace();
}
JLabel lbl = new JLabel(new ImageIcon(myPicture));
frame.add(tabbedPane);
jp1.add(Firstpanel,BorderLayout.SOUTH);
tabbedPane.addTab("New Employer",jp1);
Firstpanel.add(button1);
Firstpanel.add(button2);
Firstpanel.add(button3);
}
public static void main(String[] args)
{
frame.setPreferredSize(new Dimension(1500, 1210));
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setResizable(true);
frame.setVisible(true);
new Mainframe();
}
}
|
|
|
|
|
|
Yes now I know my mistake,thank you
|
|
|
|
|
You need to tell the panel to use a border layout like:
jp1.setLayout(new BorderLayout());
jp1.add(Firstpanel,BorderLayout.SOUTH);
tabbedPane.addTab("New Employer",jp1);
Firstpanel.add(button1);
Firstpanel.add(button2);
Firstpanel.add(button3);
jp1.add(Secondpanel, BorderLayout.NORTH);
Secondpanel.add(button4);
Secondpanel.add(button5);
Veni, vidi, abiit domum
|
|
|
|
|
Thank you for your help
|
|
|
|
|
Hi All,
I am developing an ADF Mobile app in JDeveloper that should connect to Sql Server DB. I included a driver file in C:\Oracle\Middleware\jdeveloper\jdev\lib\sqljdbc4.jar but I am still getting error when I import java.sql.DriverManager.I even included sqljdbc4.jar file in project properties. Please help me solve this problem. Thanks.
|
|
|
|
|
AMADIAR wrote: Please help me solve this problem. Solve what problem, you have not explained what the error is?
Veni, vidi, abiit domum
|
|
|
|
|
Hello,
Please explain the error you are getting.
Regards,
Prasad P. Khandekar
Knowledge exists, man only discovers it.
|
|
|
|
|
I am writing a small data driven application which takes data from an input and throw an event throughout the program that is subscribed to the data event listener. Currently, I have the small test program setup as an infinite loop to see if there are any memory leaks. Testing has shown that there is a massive memory leak and I don't know what to do. I have the program attached:
What is happening is when the event is handled, it is not properly disposed of. Instead, it is thrown onto the stack while a new event is serviced. I thought sleeping the current event would dispose of the last event but...nope! I am out of ideas on how to properly service and dispose of events quickly using this form. Any ideas would be helpful to figure this small technical issue out. Thanks - Ryan
The Main loop which has the data items and the main value observer.
import java.util.*;
public class CMain {
private final static int TIMEOUT = 250;
private static CValue Value;
private static CData AddFive;
private static CData AddTwo;
private static CData AddTen;
private static CData AddSeven;
private static CData MaxValueReached;
public static void main(String[] args) {
Value = new CValue();
AddFive = new CData();
AddFive.setItsAction(new CAction()
{
@Override
public void doAction() throws InterruptedException {
Thread.sleep(TIMEOUT);
if (Value.getItsValue() <= 20)
{
Value.setItsValue(Value.getItsValue() + 5);
}
}
});
Value.addObserver(AddFive);
AddTwo = new CData();
AddTwo.setItsAction(new CAction()
{
public void doAction() throws InterruptedException{
Thread.sleep(TIMEOUT);
if (Value.getItsValue() > 20 && Value.getItsValue() <= 40)
{
Value.setItsValue(Value.getItsValue() + 2);
}
}
});
Value.addObserver(AddTwo);
AddTen = new CData();
AddTen.setItsAction(new CAction(){
public void doAction() throws InterruptedException{
Thread.sleep(TIMEOUT);
if (Value.getItsValue() > 40 && Value.getItsValue() <= 80)
{
Value.setItsValue(Value.getItsValue() + 10);
}
}
});
Value.addObserver(AddTen);
AddSeven = new CData();
AddSeven.setItsAction(new CAction(){
public void doAction() throws InterruptedException{
Thread.sleep(TIMEOUT);
if (Value.getItsValue() > 80)
{
Value.setItsValue(Value.getItsValue() + 7);
}
}
});
Value.addObserver(AddSeven);
MaxValueReached = new CData();
MaxValueReached.setItsAction(new CAction()
{
public void doAction() throws InterruptedException{
Thread.sleep(TIMEOUT);
if (Value.getItsValue() >= 100)
{
Value.setItsValue(0);
System.out.print("Value Reset back to 0\n");
}
}
});
Value.addObserver(MaxValueReached);
Value.startBehavior();
while (true);
}
}
The CValue class
import java.util.Observable;
public class CValue extends Observable
{
private double itsValue = 0;
public double getItsValue() {
return itsValue;
}
public void setItsValue(double itsValue) {
if (this.itsValue != itsValue)
{
this.itsValue = itsValue;
System.out.print("Value set to " + itsValue + "\n");
System.out.print(false);
setChanged();
notifyObservers();
}
}
public void startBehavior()
{
setChanged();
notifyObservers();
}
}
The CData Class
import java.util.Observable;
import java.util.Observer;
public class CData extends Observable implements Observer
{
public CData()
{
}
@Override
public void update(Observable arg0, Object arg1) {
if (itsAction != null)
{
try {
this.itsAction.doAction();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private CAction itsAction = null;
public CAction getItsAction() {
return itsAction;
}
public void setItsAction(CAction itsAction) {
this.itsAction = itsAction;
}
}
And to explain what this CAction thing is:
public interface CAction {
public void doAction() throws InterruptedException;
}
|
|
|
|
|
I think you failed to understand the publisher-subscriber framework. Publisher has many subscriber .i.e one to many relation ship with subscriber.Publisher post the new event(if any), and all the subscriber which have the subscription, will catch the event. So this is pretty much concept. In java , publisher as Observable and subscriber as Observer. Now, in your app you create an Observable (CValue). Then you create Observer which is again an Observable(CData extends Observable) and register this with the Observable, means you register an Observable with another Observable. So fixed this things before moving forward
Regards
Shubhashish
|
|
|
|
|
Shubhashish, thank you for taking the time to analyze and reply to my original message. I am learning new concepts in Java to become a better programmer.
I have taken you comments into serious consideration and updated the code. The class headers for CValue and CData are as follows:
public class CValue extends Observable
public class CData implements Observer
I would execute the test program for a minute to two minutes before its crashes out. What I am observing during execution is this:
CMain.doAction()
CValue.setItsValue(double)
CValue.notifyObservers()
CData.update(Observerable,Object)
CMain.doAction
...
Repeat until thread crash
It seems the events are not being closed completely after the servicing of the observer. I know I am creating a lot of events being generated quickly but isn't there a mechanism which would close these events or am I not closing them down properly myself?
Ryan
|
|
|
|
|