Results 1 to 2 of 2
Thread: Java Server Client C problem
- 01-13-2011, 02:21 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
Java Server Client C problem
I'm having a problem with my server and client, as a result he seems to bind evything and so... But I can't send from my client or receive... Don't seem to find the wrong thing myself...
Can anyone please help
This is the Java
public class startServer {
public static void main(String[] args) {
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = null;
try {
welcomeSocket = new ServerSocket(6789);
System.out.println("Server running.");
while(true) {
Socket connectionSocket = null;
connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream( )));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream( ));
System.out.println("receive...");
outToClient.writeBytes("test");
clientSentence = inFromClient.readLine();
if(clientSentence.startsWith("TOTAAL_BEDRAG:")) { // eerste deel = 14, tweede 8
capitalizedSentence = clientSentence.substring(14).toUpperCase() + "\n";
System.out.println("VALID MESSAGE: \"" + clientSentence + "\"");
outToClient.writeBytes(capitalizedSentence);
} else {
System.out.println("INVALID MESSAGE: \"" + clientSentence + "\"");
outToClient.writeBytes("\n");
}
}
} catch (IOException e) {
System.out.println("Error: Java");
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
//System.out.println("Received: " + clientSentence);
//outToClient.writeBytes(capitalizedSentence);
}
}
This is the Client
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
__declspec(dllexport) int client(char *B1_REKNUM) {
// Declaraties voor conecteren
WSADATA wsaData;
SOCKET client;
SOCKADDR_IN service;
// Declaraties voor verzenden
int bytesSend;
int bytesRecv;
char sendbuf[100] = "1";
char recvbuf[100] = "";
int * p_int;
// MAKEWORD(1,1) for Winsock 1.1, MAKEWORD(2,0) for Winsock 2.0:
printf("\n\n");
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
printf("Client ERROR: WSAStartup failed.\n");
WSACleanup();
exit(1);
} else {
printf("Client info: WSAStartup succes.\n");
}
// Socket maken
client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(client==INVALID_SOCKET) {
printf("Client ERROR: Socket failed.\n");
WSACleanup();
exit(1);
} else {
printf("Client info: Socket succes.\n");
}
p_int = (int*)malloc(sizeof(int));
*p_int = 1;
if( (setsockopt(client, SOL_SOCKET, SO_KEEPALIVE, (char*)p_int, sizeof(int)) == -1 )||
(setsockopt(client, SOL_SOCKET, TCP_NODELAY , (char*)p_int, sizeof(int)) == -1 ) ){
printf("Error setting options %d\n", WSAGetLastError());
} else {
printf("SETSOCKOPT: OK");
}
// Adres samenstellen
service.sin_family = AF_INET; // Family is internet adres.
service.sin_addr.s_addr = inet_addr("127.0.0.1"); // IP adres voor connectie.
service.sin_port = htons(6789); // Port voor de connectie.
// Binding
if(bind(client, (SOCKADDR*)&service, sizeof(service)) == SOCKET_ERROR) { // Uiteindelijk binding
printf("Client error: Bind failed.\n");
closesocket(client);
exit(1); // return 0;
} else {
printf("Client info: Bind succes.\n");
}
// Verzenden van data
bytesSend = send(client, sendbuf, strlen(sendbuf), 0);
if(bytesSend = SOCKET_ERROR) {
printf("Client ERROR: Send failed: %ld\n", WSAGetLastError());
closesocket(client);
exit(1);
} else {
printf("Client info: Send succes.\n");
printf("Client info: Bytes send is: %ld\n", bytesSend);
printf("Client info: Send data is \"%s\"\n", sendbuf);
closesocket(client);
}
// Ontvangen van data
bytesRecv = recv(client, recvbuf, sizeof(recvbuf)-1, 0);
if(bytesRecv == 0 || bytesRecv == WSAECONNRESET) {
printf("Client ERROR: Connectie gesloten.\n");
}
if(bytesRecv < 0) {
printf("Client ERROR: Recv failed.\n");
return 0;
} else {
recvbuf[bytesRecv] = '\0';
printf("Client info: Recv succes.\n");
printf("Client info: Received data is: \"%s\"\n", recvbuf);
printf("Client info: Bytes received is: %ld.\n", bytesRecv);
}
printf("Einde c...");
WSACleanup();
return 0;
}
- 01-13-2011, 02:32 PM #2
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Client/Server Serialization Problem
By JDaniel in forum NetworkingReplies: 0Last Post: 12-17-2010, 08:34 PM -
Java Socket server with C client problem
By rnvrnv in forum NetworkingReplies: 6Last Post: 11-09-2010, 12:47 AM -
Problem when communicating java server and C client application
By tdnghia89 in forum NetworkingReplies: 0Last Post: 10-18-2010, 05:18 AM -
about client to server problem
By raywai in forum New To JavaReplies: 3Last Post: 05-13-2010, 04:34 PM -
client server problem
By kievari in forum Advanced JavaReplies: 4Last Post: 02-12-2010, 08:06 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks