Passing A Value For Sockets?

Mar 13, 2014

I am having a problem passing an int value from one class to the other. The gist of the objective of this program is to compute a local sum at each client, and then once the local sums are computed.. to compute the overall sum from all the clients. My problem is that I'm having trouble figuring out how to pass each sum from each individual client in the class multicastSenderReceiver to the readThread class. I keep getting an error.

I feel that I have to be able to pass the sum value from each client generated in multicastSenderReceiver to the other class, because that is the only way I'll accurately be able to sum up all the values..

Java Code:

package multicastsenderreceiver;
import java.net.*;
import java.io.*;
import java.util.*;
//import static multicastsenderreceiver.multicastSenderReceiver.numProcesses;
class multicastSenderReceiver{

[code]....

View Replies


ADVERTISEMENT

How To Connect With Sockets From Other PCs

Dec 26, 2014

I have just started learning about sockets and such... and i have created a chat program with a server and a client

server:

public class Server {
private static ServerSocket server;
private static Socket connection;
private static PrintWriter pw;
private static BufferedReader br;
private static JTextField textOutput;

[Code] ....

so, everything is working fine . There is only one thing i would like to do now: make it so that i can run the server from my computer and then others can run the client from theirs and we will be able to chat . So I tried:

In the client code on line 56 is says:

connection = new Socket("localhost",7777);

So I changed "localhost",7777 to "myip",7777)

But when i run the server on my computer and run the client on another computer i get this error:

java.net.ConnectException: Connection refused: connect

Why is the connection getting refused? is it for security reasons? so hackers cant connect to me or something? And is there a way to tell your computer to allow that client to connect?

View Replies View Related

Network Communication Using Sockets?

Dec 6, 2014

I have all of my code written, but it is not producing any output and i'm not sure why.

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;

[code]...

View Replies View Related

Using HTTP Requests For Sockets Instead Of TCP

Apr 11, 2014

Generally Socket Programming uses HTTP Protocol to send and receive messages in Java. Can we try to exchange HTTP/HTTPS requests instead of TCP?

a Socket constructs a HTTP request to send to another socket from information it has and sends to another one and Server responds with HTTPResponse.

View Replies View Related

How To Minimize CPU Usage For Sockets And Threads

Aug 16, 2012

I created an instant messenger using java. When I have the Server that communicates between the clients and one client running on my Computer the CPU Usage is at 100%. It really slows down everything else I'm doing and I figure this might be an issue if I gave this to people to use. I don't want the client taking up a lot of CPU Usage if they're just running it in the background while doing other things on their computer. The program utilizes multithreading. Each thread is constantly being polled for input.

The Server, as seen below, has two threads. I explain what the threads do before the code. There is also another while loop running constantly in the server that is waiting for sockets to connect. The loop does not run constantly at the line socket.accept(); it stops and just waits.

The User, split into a menu and chat window, has two threads as well. I explain what the threads do before the code. After I originally posted I put a 100 ms sleep in all my threads. CPU Usage is still at 100%*

This thread listens for input from the user. The input tells the server what action to take. There is a thread running for every user currently connected to the server.

public void run()
{
try
{
input = new DataInputStream(user.getSocket().getInputStream());
output = new DataOutputStream(user.getSocket().getOutputStream());

[code]....

View Replies View Related

File Transfer In Java Using Sockets

Apr 8, 2014

I have written a java code to transfer files from one server to another using the concept of socket programming. I got the codes from another java forum that meet my requirements. The program is said to transfer large sized files (like .mkv , .mprg movies) from one machine to another and can be used to transfer files of all formats. But after running the codes I found that the program is not able to transfer large sized files such as movies and even pdf of sizes 80mb or 111mb. When i transfer small sized files, it gets transferred and the output shows that. You can run the codes and observe it. But when i try to transfer large sized files, the program goes on running for hours. The large sized files are not getting transferred. The program has used bytebuffer but still this error occurs.

**ClientMain.java**
import java.io.IOException;
import java.net.Socket;
public class ClientMain

[code]....

Note that:-

1. ClientMain.java and DirectoryTxr.java are the two classes under client application.
2. ServerMain.java and DirectoryRcr.java are the two classes under Server application.
3. run the ClientMain.java and ServerMain.java simultaneously

Also specify the source directory, destination directory and host address of the machine in which server is running in the ClientMain.java(as per your computer). Here we are not specifying source file ,instead a source directory or folder is specifying.So the entire files of source directory will be transferred.

View Replies View Related

Java UDP Sockets And Multi-Threading

Dec 26, 2014

I have been practicing writing java code, my university course is going to cover socket programming and multi-threading.

I am presently just starting to write myself a framework for all multiplayer games I may make in the future, my aim really is simply to practice and understand better and this time, im not using an ide, just sublime text, all new grounds for me, I have a good basic understanding of the subject but I want to be fluent.

import java.net.*;
import java.util.ArrayList;
import java.io.*;
/*SMOOTH THREAD SAFE MULTI CLIENT HANDLING CLASS.
*
*This class will create connection objects when a connection is detected, these connections will run in a separate thread and update an array list in their parent class containing their last sent data

[Code] .....

View Replies View Related

Sockets Can Be Used To Transfers File Over Network

May 7, 2014

This question has confused me because "Errors" are capitalized and I'm not sure what they mean.

To me this question is false. Sockets can be used to transfers file over a network.

I can't see why this wouldn't work. The code looks fine to me.

I don't think this is true unless you're using the mouseEntered() or mouseExited() method. But again, I'm not sure.

I think this is true, I don't know what else could have listeners.

They're only worth a few marks each so the answer is only suppose to be a line or two. They are all true or false questions that require an explanation.

View Replies View Related

Communicating Between Client And Server Sockets In Java

Apr 3, 2014

I'm working on an assignment right now which involves passing a variable series of numbers to the Server to be sorted into the correct order and returned to the client.

So far I have it connecting to the server and asking for my numbers, and I am entering each number and pressing return, and it is accepting each number individually.

One (smaller I think) problem i'm having is with the client code, specifically the while loop.. I've tried to code it so that when I enter a full stop ('.') the program will stop asking me for more numbers and move on to sending them to the server.. but instead it just crashes giving an error.. heres an example

"Enter a number: 1
Enter a number: 3
Enter a number: 2
Enter a number: 1.
java.lang.NumberFormatException: For input string: "1."
at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)

[Code] ....

Here's the client code in full

package numberSortprogram;
import java.io.*;
import java.net.ServerSocket;
public class NumSortClient {
public static void main(String[] args) {
InputStreamReader is = new InputStreamReader(System.in);
[Code] ....

The big problem that I have is figuring out how to send the sorted array back to the client..

So, what im trying to get to happen is-

-Client is asked to enter numbers
-Client enters series of numbers into an array
-Array is converted to a string and sent to the server
-String is then split into an array of strings and sorted using compareTo (don't know if this is the best way?)
-Sorted array is sent back to client and displayed.

fyi this is making use of streamsockets..

All of the rest of my code is below:

package numberSortprogram;
import java.io.IOException;
import java.net.*;
import java.util.regex.PatternSyntaxException;
public class NumSortServer {
public static void main(String[] args) {
int serverPort = 4444; // default port
if (args.length == 1 )

[Code] .....

View Replies View Related

Java Solution To Communicate Via Applications On Without Sockets

Oct 6, 2014

I have an application written in Java on the Linux platform. My application will work the following way:

User A will open application. User B will open application.User A will need to send User B a message but without a socket connection.User B will need to send User A a message but without a socket connection.The user should be able to identify the messages sent to each other.If User A reads user B's message the message will no longer be available in the channel of communication.If one user exits their application their message should be removed.User C should not be able to read user A and user B message (This is only via the application design, no real security here).Applications should be able to work on different machines however they will utilize a shared network mount to access files modified by each other.

I do have to note that the messages being sent is rather small and only 1 message is sent from each user, so in that regard I did not want to setup a client/server model to do this using sockets.

Basically I am looking for a similar concept as a message queue but more relevant to my requirements done in Java. What are some good options to use that will address some of my requirements? I have not touched Java in a long time and only have used it for certain usage so I am trying to get an idea of which current technologies are best for what I need.

View Replies View Related

File Empty When Attempt To Transfer Between Computers Using Sockets

May 26, 2014

I am working on a project with client/server relationship and I want the client to be able to transfer their personal music files over the socket. Every time I attempt to do it the file is created but it is empty.

Server code:

Java Code:

public class Server extends JFrame {
//===============================
// FIELDS
//===============================
// connection essentials
private ServerSocket server;
private Socket connection;

[Code] ....

View Replies View Related

Value Not Passing From Second Class?

Nov 8, 2014

I am trying to pass value from class to another using net beans but it seems that I am doing something wrong.

find my basic code at sourceforge.net/projects/val1toval2/files/SRC/

I would like to have both VAL1 and VAL2 having same value in Jlabel.

//Main.java 
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Second SEC = new Second();
l_dis.setText(String.valueOf(getInput()));
l_tot.setText(String.valueOf(SEC.getTotal()));

[code]...

View Replies View Related

JSP :: Passing ID To Servlet Through URL

Mar 9, 2015

I was send the id through url like this code.

<td> <%=rs3.getString("Project")%> </td> <td> <a href="TaskAssignment.jsp?id=<%=rs3.getString("id")%>"> <input type="button" name="edit" value="Edit"></a></td>

i have problem to getting this id in servlet.

Servlet:

String id=request.getParameter("id");

the variable id getting only null

View Replies View Related

Passing A Map As Parameter

Apr 30, 2014

I'm using Java in BlueJ where I'm trying to write a class to store cycling club membership details in a map. I'm trying to pass a map as a parameter to a function that will iterate over the map and print out the member details. My code is:

public class CtcMembers
{
//instance variables
private Map<String, Set<String>> memberMap;
/**
* Constructor for objects of class CtcMembers

[Code] ....

When I execute the following in the OU workspace:

CtcMembers c = new CtcMembers();
c.addCyclists();

I can see a map populated with the expected details.

When I execute the following in the OU workspace:

c.printMap(c.getMap());

I get the following error:

java.lang.ClassCastException: java.util.HashSet cannot be cast to java.lang.String
in CtcMembers.printMap(CtcMembers.java:81)
in (OUWorkspace:1)

I wasn't aware I was trying to cast anything so this has got me baffled.

View Replies View Related

C++ Passing By Value With Two Variables

Jun 24, 2014

This is my first time working with C++ and I have put together this program and came up with two errors and I am unsure what it is wanting me to do. The errors I got are:

1>c:usersownerdocumentsvisual studio 2010projectsweek5week5passing_by_value.cpp(30): error C2064: term does not evaluate to a function taking 1 arguments
1>c:usersownerdocumentsvisual studio 2010projectsweek5week5passing_by_value.cpp(38): error C2064: term does not evaluate to a function taking 1 arguments

#include<iostream>
using std::cin;
using std::cout;
using std::endl;
//initialize arrays
int incr10(int* numa,int* numb);

[Code] ....

View Replies View Related

Passing A Value Into A Method

May 8, 2014

Does the following code pass a value into a method?

/code

lnValue[x][y] = computelnValue();

View Replies View Related

JSF :: Passing Parameters From Servlet

Sep 12, 2014

I'm trying to pass a parmeter to a jsf page from a servlet(i.e. associated with paypal adaptive api), but I keep getting the following error, even though the productType on ProductDetailsVO is a String.

INFO: WARNING: FacesMessage(s) have been enqueued, but may not have been displayed.

sourceId=j_idt2[severity=(ERROR 2), summary=(j_idt2: '45;productType=Sport'

must be a number consisting of one or more digits.), detail=(j_idt2: '45;productType=Sport' must be a number between -2147483648 and 2147483647 Example: 9346)]

Calling JSF page contains:-

returnURL = new URL(new URL(request.getRequestURL().toString()),"pages/paypalpaymentapproved.xhtml?paypalID="+paypalID+";productType=Sport");
response.sendRedirect(returnURL.toString());

[Code] ....

View Replies View Related

Passing Value From JTextField In One Class To Another

Apr 6, 2015

I enter a value in the text box in one class and want to pass that value to another class in order to compare it and color the cell in a table. In the first class I have

package cege.ui;
 import cege.controller.HtmlController;
import cege.controller.ScreenCaptureController;
...
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

[Code] .....

How to pass the threshhold from the first to the second class?

View Replies View Related

Passing A Value From Applet To JavaScript

Feb 5, 2014

I have a java application in which when starting it a map is opened and a screen is captured. The capturing is in certain time interval. I want to enable the user to change the interval for the automatic capturing the map. I placed a text box in a JPanel. I want to pass the value which was entered in it to the html page which opens a Google map. Since I am new in java programming, I found that I can do it by using applets. I made an applet, and tried to call it in the html file. But now instead of opening the map file in a browser, it opens very quickly only black window.

Here is my applet:

package cege.ui;
import java.applet.Applet;
import java.io.IOException;
import cege.ui.GuiMain;
public class AppletTInterval extends Applet {
private int TimeInterval=0;

[Code] .....

And the code in the html file where I try to call the applet, i.e. to pass the value which is returned by the applet is:

<script src=
<!--"https://www.java.com/js/deployJava.js"></script>
<script>
var attributes = { id:'AppletTInterval',
code:'cege.ui.AppletTInterval', width:1, height:1} ;
var parameters = { jnlp_href: 'AppletTInterval.jnlp'} ;

[Code] .....

Can I use this way to pass a value from the java class file (from the JTextField into the html file?

View Replies View Related

Passing Values To Different Methods?

Mar 1, 2015

I am trying to pass the values for UPPER_BOUND and LOWER_BOUND from the main method into the getValidNumber method. However, I'm not sure how to do this. The rest of the code is correct as far as I know, I just need to get the values for the bounds into the getValidNumber method. How would I do this? the notes in the main method explain what I need to do.

public static void main(String[] args) throws FileNotFoundException {
Scanner kb = new Scanner (System.in);
final double LOWER_BOUND = 0;
final double UPPER_BOUND = 100;
//Call the getValidNumber method, passing it the values LOWER_BOUND and UPPER_BOUND.
//Take the returned value and store it in a variable called num.
//Print out the value of num (here in the main method)
 
[Code] .....

View Replies View Related

Passing Object As Parameter?

Aug 8, 2014

i want to pass an object of type Software to assign it to a computer from Computer class...

i should note that computer and software are arrays of objects thats the variable and method to set software in Computer class

private Software[] software;
public void setSoftware(Software software,int index){
this.software[index]=software;}

note: the user choses the a computer from a list and a software as will

for example the program will show the user 2 computers

0 for computer: apple, Model: mac mini, Speed: 2.8

1 for computer: sony, Model: vaio, Speed: 2.2

the user enters the index he wants then the program will show a list of software to add to the computer selected

the error I'm having is run time error Exception in thread "main" java.lang.NullPointerException and it points to these 2 lines

1.comp[Cch].setSoftware(software,Sch);

2. the method setSoftware

every thing is running correctly but this step above

Cch= the chosen computer index

Sch= the chosen Software index

why am i getting an error and how to fix it?

View Replies View Related

Passing In Variable From One Class To Another?

Aug 16, 2014

It just wont work... Everytime I try to pass a variable into a class it sets it to 0. What to do.

The variable should be '2', but it prints out as 0!?

Java Code: package Zoo;
import java.util.Random;
public class Animal {
Random random = new Random();
public void Eat(){
System.out.println("The animal starts to eat.");

[Code]...

The method bash, needs the variable 'playerTwo' to be 2, to carry on. But it prints out 0.

Int the animal constructor, I passed in the variable from another class, and it prints out 2 in the constructor like it should. But outside of the constructor it is equal to 0.

View Replies View Related

Getting Values From One Class To Another Without Passing Into It

Feb 19, 2015

I am trying to get the username and password for one class and send them to another. However I cannot send them in the constructor because a database class is getting them and I need that constructor blank. However if I do not pass the class in the constructor I get a NULL error, even better when I do pass it in I seem to get a thread lock. I do try to do my own housekeeping and close the one frame before I open a new one but it doesn't work. I will do my best to show the design of the program.

program starts
Java Code: public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// connects to database and generates a GUI
new UserPromptScreen();

[code]....

I have to pass in UserPromptScreen in for it to work. This will cause me issues later with my design.

View Replies View Related

JSF :: Passing Session To Different Machine

Aug 4, 2014

In my JSF application user starts on primary server where the session begins and then the user is redirected to a different server using sendRedirect. I want to pass some authentication token to the next server from primary server. I am trying to set session attribute as:

request.getSession().setAttribute("auth_token", "1");

And then send it to the next server as:

response.sendRedirect(encodedUrl);

But this attribute is not reaching the new server. I cannot pass this auth_token as request parameter as that wont be secure. So how to get some session data to new server?

View Replies View Related

Passing Values To Methods?

Apr 30, 2014

I'm supposed to write a program that reads in 20 numbers stores them into a one dimensional array and then create a method that will calculate the average of the numbers in a separate method.

I've written a for loop in the main method that will take in the numbers but now I need to know how I can pass those values to a method that will calculate the average.

public class ConstructorHomework {
private static double average; //Declaring the Global Variable
//This Method Calculates The Average
public void avg(int x){
average = x/20;

[Code] .....

This is the main method an it contains the for loop that will take in 20 numbers

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
final int size = 20;
double[] array = new double[size];
for (int i = 0; i < array.length; i++){
array[i] = input.nextDouble();
}
//

Here I was trying to create and object and use it to pass value to the method

ConstructorHomework a = new ConstructorHomework();
a.avg(); //Problem is that I don't know what to put in the brackets
a.showNum();
}
}

View Replies View Related

Java Code For Passing By Value

Oct 7, 2014

you will be making four variables (2 primitive integers and 2 Account Objects). You will be following the steps detailed below. The challenge is to understand exactly what is happening and why. Start by coding the following two methods in a java file.

Start your main method with four variables: a, b c, d. a and by should be integer data, set them to be the values 10 and 20 respectively. Variables c and d should be Account Objects. The first account © is Ted, has $70,000 as his balance. The second account (d) is Joe, he has only $15,000 as his balance. Their account numbers are up to you to determine.Next, please print out the four variables: a, b, c, and d. Make sure that you label which ones are which. After you print these variables, please pass all four variables to a static method that you will create below. After you finish the method, please print out the four variables again: a, b, c, and d. Just as they were printed before the method.

Static methods: This method can be called whatever you want, however it will be public, static, and void. This method should take in four parameters: w and x which will be integers, then y and z which will be Account Objects.The first thing this method should do is to print out the four variables w, x, y, and z. This should be done in the same manner as you did in the main method. Next, change the first variable (w) to 15. After that, ted bought a new car. He now has only $20,000 in his account. Change the balance of y to be 20,000. Step three is to make a new Account Object named John, he has a balance of $10,000. Assign him to the variable z. Lastly, print all four variables again: w, x, y, and z.

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved