Link to home
Start Free TrialLog in
Avatar of kayvey
kayvey

asked on

what the hey

oh mwan.  these sockets are boingy boingy.

here is command line happenings:

 9264  p1  I+     0:00.04 vi BankServer.java
> gcc bank_client.c -o bank
> java BankServer &
[1] 12575
> Bank is open!



Bank>

Bank>

./bank
teller received msg:
 null
Exception in thread "main" java.lang.NullPointerException
        at BankServer.main(BankServer.java:29)
Bus error (core dumped)


[end command line happenings]

here is java code

import java.io.*;
import java.net.*;
import java.util.*;

public class BankServer
{
  BankServer()
  {
    balance = 0;
  }
  private int balance;

  public static void main(String args[]) throws IOException
  {
    ServerSocket bankDoor = new ServerSocket(6669);
    String patronBlahBlah;
    System.out.println("Bank is open!\n");
    System.out.println("\n");
    System.out.println("Bank>\n");
    while (true)
    {
      System.out.println("Bank>\n");
      Socket teller = bankDoor.accept();
      BufferedReader patronReq =
       new BufferedReader (new InputStreamReader(
        teller.getInputStream()));
      patronBlahBlah = patronReq.readLine();
      System.out.println("teller received msg:\n "+patronBlahBlah);
      if (patronBlahBlah.equals("bye"))
      {
        System.out.println("teller says bye\n ");
        break;
      }
      else
      {
        System.out.println("bank staying open\n ");
      }

    }
    System.out.println("Bank closing\n");
  }

  private void deposit(int amount)
  {
    if (amount >= 0 )
    {
      balance += amount;
    }
    else
    {
      System.out.println("Use withdraw take money out.\n");
      System.out.println("$ ");
      System.out.println(amount);
      System.out.println(" not accepted as deposit\n");
    }
  }

  private void withdraw(int amount)
  {
    if (amount >= 0 )
    {
      if (amount <= balance)
      {
        balance -= amount;
      }
      else
      {
        System.out.println("Insufficient funds for withdrawl of\n");
        System.out.println("$ ");
        System.out.println(amount);
        System.out.println("Withdrawl not accepted.\n");
      }
    }
    else
    {
      System.out.println("Use deposit to get money.\n");
      System.out.println("$ ");
      System.out.println(amount);
      System.out.println(" not accepted as withdrawl\n");
    }    

  }
 
  private int balance()
  {
    return balance;
  }
}

[end java code]

here is c code

#define theSOCKET "bank_sock"
#define bankPORT 6669

#include <sys/types.h>
#include <stdio.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <string.h>
#include <netdb.h>

int main (void)
{
  char buffer[1024];
  char * msg;
  int sock, length;
  char bankHost[64];
  struct hostent *ptrHost;
  struct sockaddr_in tellerWinder;

  /*
   * get localhost
  if (gethostname(bankHost, sizeof(bankHost)) < 0)
  {
    perror("can't get localhost\n");
    exit(45);
  }
   */

  /*
   * call post office to get zip
   */
  if ((ptrHost =  gethostbyname("localhost")) == NULL)
  {
    fprintf(stderr, "can't find host: %s \n",bankHost);
    exit(46);
  }

  /*
   *  attempt to create INET socket
   */
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  {
    perror("can't create socket\n");
    exit(42);
  }

  /*
   *  transcribe bank statement into Yahoo! maps
   */
  memset(&tellerWinder, 0, sizeof(struct sockaddr_in));
  tellerWinder.sin_family = AF_INET;
  tellerWinder.sin_port = htons(bankPORT);
  memcpy(&tellerWinder.sin_addr, ptrHost->h_addr_list[0], ptrHost->h_length );
  length = sizeof(struct  sockaddr_in) ;

  /*
   *  download Yahoo! map doc into cigarette lighter
   */
  if (connect(sock, (struct sockaddr *) &tellerWinder, length) < 0)
  {
    perror("can't connect to tellerWinder\n");
    exit(43);
  }


  /*
   * werking 9 to 5, what a way to make a living
   */
  msg = malloc(6);
  strcpy("hello",msg);
  printf ("msg is %s \n",msg);
  printf("in client before while\n");
 while (!strcmp(msg,"bye"))
  {
    free(msg);
    printf("doing while\n");
    length = read(0, buffer, sizeof(buffer));
    buffer[length]='\0';
    length += 1;
    msg=malloc(length);
    strcpy(buffer,msg);
    printf("client msg: %s\n",msg);
    if(send(sock, msg, length,0) < 0)
    {
      perror("client can't talk to tellerWinder\n");
      free(msg);
      exit(44);
    }
  }
  close(sock);
  exit(0);
}

[end c code ]

running java like this

java BankServer &

and c like this

./bank

umm..
Avatar of kayvey
kayvey

ASKER

the freekin' java dingy exits when i compile

> javac BankServer.java
> java BankServer &
[1] 13832
> ./bank
Exception in thread "main" java.lang.NullPointerException
        at BankServer.main(BankServer.java:29)
Bus error (core dumped)
> gcc bank_client.c -o bank
[1]  + Exit 1                        java BankServer
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> patronBlahBlah = patronReq.readLine();

check whether patronReq.readLine() has any data before doing further operations..
Avatar of kayvey

ASKER

i figgerd it out myself.  i had a c problem.  heres a latested output

msg is hello
in client before while
doing while
bye
client msg: bye

> gcc -o bank bank_client.c
> ./bank
in client
in client got host
in client got socket
in client addressed socket
in client connected to socket
in client malloced
in client strcpy'ed
msg is hello
in client before while
doing while
^C
> gcc -o bank bank_client.c
> ./bank
can't connect to tellerWinder
: Connection refused
>

oops that's the c section.  objects.. yeah.. okay dood u were helpful in the other dingy but not
as much as the c guy with the conflict of interest sunnycoder?  

okie dokie then.. here's yer points.. yer comment was helpful to me. instilled confidence.
Avatar of kayvey

ASKER

btw this bit here

Bus error (core dumped)

was the c program talking i think.  i decided to put them in their own little terminals
instead of using java BankClient & command to run in the background. too confusing.
Avatar of kayvey

ASKER

the problem was that the client from the c program was sending a
NULL message to the java program.  so i guess it was a c problem
but it produced a java error.  

 while (strcmp(msg,"bye\n"))
  {
    free(msg);
    length = read(0, buffer, sizeof(buffer));
    buffer[length]='\0';
    length += 1;
    msg=malloc(length);
    strcpy(msg,buffer);
    printf("patron msg: %s\n",msg);
    if(send(sock, msg, length,0) < 0)
    {


the problem was in this bit of code.  i was using strcpy on buffer wrong.  i needed
to reverse the order of msg and buffer.  it is acceptable to use a char array for
a source but not a destination for strcpy i think.  i have encountered that behavior
before.  because strcpy failed, msg was equal to NULL.  the NULL msg was sent
to the java program via the send command u see above.  that produced the java
error.

hmm.. usually u have to initialize char * to NULL to get null.. that's funny.  maybe
freeBSD 6.1 is doing it for me?
Avatar of kayvey

ASKER

turns out it was sorta a c problem.  admin dood.  bwahahahahaha

maybe i should try answering some questions here sometime.
Avatar of kayvey

ASKER

i decided to take yer advice.  trippy "bye" is actually an object with functions!!?!?!!  {:D
> Bus error (core dumped)
> was the c program talking i think.  

yes, java app shouldn't do that
Avatar of kayvey

ASKER

i'm really fixated on that string literal have object properties tho.  wowsville.  outasite.